我正在使用ajax在控制器中调用一个函数。如果控制器中有错误,它会进入Error.cshtml页面,从该页面发送错误电子邮件,但它不会在屏幕上呈现Error.cshtml页面,它会停留在带有ajax调用的页面上
我该如何解决?
这是ajax调用:
$("#County").change(function () {
$.ajax({
url: '@Url.Action("UpdateCountySessionVariables", "Home")',
type: 'POST',
data: JSON.stringify({ countyId: $("#County").val() }),
datatype: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
location.reload();
},
failure: function (data) {
alert('failure');
},
});
});
以下是主web.config中的customErrors:
<customErrors mode="On" defaultRedirect="~/Error" redirectMode="ResponseRedirect"/>
答案 0 :(得分:0)
在$.ajax
定义中,您应使用error
属性,而不是failure
,请检查documentation。像这样:
$.ajax({
url: '@Url.Action("UpdateCountySessionVariables", "Home")',
type: 'POST',
data: JSON.stringify({ countyId: $("#County").val() }),
datatype: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
location.reload();
},
error: function (data) {
alert('failure');
},
});
您还可以获得如下所示的完整错误页面:
error: function (httpRequest) {
console.log(httpRequest.responseText);
},