所以我的页面上有一个链接导致一些代码中断。代码抛出异常,导致我的代码跳转到控制器上的“Catch”块。 catch块如下所示:
catch (Exception e)
{
return Json(e.ToString());
}
发生的事情是,我在前端看到了这个错误:
This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.
现在,如果我将上述return语句的行为更改为以下内容:
return Json(e.ToString(), JsonRequestBehavior.AllowGet);
在前端,我实际上得到一个弹出式对话框,询问我是否要从localhost下载一个Json项目,而且我不会得到更具描述性的错误。
这里发生了什么以及如何解决?
答案 0 :(得分:0)
需要使用AJAX调用返回JSON的操作。如果您只是在页面中指向此操作的链接,因为服务器返回JSON,浏览器不知道如何处理它。因此,例如,如果您使用的是jquery,则可以使用$.getJSON()
方法来调用此操作:
$(function() {
$.getJSON('<%= Url.Action("SomeAction") %>', { }, function(result) {
// do something with the result
alert(result);
});
});
当您描述的场景可能发生时,我可以想到另外两种情况:
.click()
回调中返回false来取消默认操作,浏览器只需按照发送GET请求的链接