【问题标题】:Why is this basic json call not working?为什么这个基本的 json 调用不起作用?
【发布时间】:2011-11-07 16:00:52
【问题描述】:

以下 JSON 调用总是命中 AJAX 错误处理程序,我不知道为什么:

$.ajax({
    type: "GET",
    url: '<%= Url.Action("MyTest", "Detail") %>',
    dataType: "json",
    error: function (xhr, status, error) {
        alert(xhr + " | " + status + " | " + error);
    },
    success: function (json) {
        alert(json);
    }
});

我得到的只是Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:4497/Detail/MyTest?_=1320681138394

当我在控制器中设置断点时,我可以看到它正在到达,所以我不确定请求在哪里下降。这是 DetailController 中的操作:

Function MyTest() As JsonResult
    Return Json("Hello")
End Function

有什么想法吗?

【问题讨论】:

标签: ajax vb.net json model-view-controller


【解决方案1】:

jQuery 错误处理程序被触发,因为它未能将返回的页面解析为有效的 JSON。由于您收到 500 内部服务器错误,因此该页面很可能不包含有效的 JSON。

在浏览器中加载此页面并修复它,直到它提供有效的 json:

http://localhost:4497/Detail/MyTest

在浏览器中给出有效的 json 后,尝试 jQuery ajax 调用。

我什至不知道 VB.NET 的基本功能,但你能不能让http://localhost:4497/Detail/MyTest 上打印的唯一内容是:

print( '{"message":"hello"}' );

如果页面输出的唯一内容是{"message":"hello"},则不会触发 json 错误处理程序

【讨论】:

  • 宾果游戏!通过浏览器加载该 URL 会显示真正的错误消息:“此请求已被阻止,因为在 GET 请求中使用此信息时可能会将敏感信息泄露给第三方网站。要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。”我所要做的就是在控制器操作中返回“Json(“Hello”,JsonRequestBehavior.AllowGet)”,它工作得很好。谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-08-16
  • 1970-01-01
  • 2014-03-07
  • 1970-01-01
  • 2014-09-18
  • 2015-06-16
  • 2016-04-28
  • 1970-01-01
相关资源
最近更新 更多