异步ajax调用错误不会显示Error.cshtml

时间:2015-03-09 18:59:05

标签: c# ajax asp.net-mvc

我正在使用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"/>

1 个答案:

答案 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);
    },