2011-11-07 95 views
0

以下JSON电话总是打AJAX错误处理程序,我想不通为什么:为什么这个基本的json调用不起作用?

$.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 

任何想法?

+0

当直接访问'http:// localhost:4497/Detail/MyTest'时会发生什么? – Sorpigal

+0

直接在您的Web浏览器中访问“http:// localhost:4497/Detail/MyTest?_ = 1320681138394”以获取有关该异常的更多信息。 –

回答

3

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错误处理程序不会触发

+2

宾果!通过浏览器加载该URL显示了真正的错误消息:“此请求已被阻止,因为在GET请求中使用此信息时,可能会向第三方网站泄露敏感信息。要允许GET请求,请将JsonRequestBehavior设置为AllowGet。”我所要做的只是在控制器操作中返回“Json(”Hello“,JsonRequestBehavior.AllowGet)”,它工作得很好。谢谢! – gfrizzle