0

我想在发生错误但不是(403、500 或 410 代码)时添加(默认)操作:

    $.ajaxSetup({
        statusCode: {
            403: function () {
                window.location = '@Url.Action("LogOn", "Account", new {area = "", msg = "forbidden", returnUrl = HttpContext.Current.Request.RawUrl})' + window.location.hash;
            },
            500: function() {
                window.location = '@Url.Action("AccessDenied", "Error")';
            },
            410: function() {
                window.location = '@Url.Action("Deleted", "Error")';
            }
            // ANY OTHER ERROR CODE - but it doesn't work, how can i do it?
            if not any above and it's an error then =>
            window.location = '@Url.Action("Index", "Error")';
        }
    });
4

2 回答 2

2

您应该ajaxError()用来处理错误而不是 ajaxSetup()

$(document).ajaxError(function(event, xhr, options) {
     switch (xhr.status)
     {
        case 403:
            window.location = '@Url.Action("LogOn", "Account", new {area = "", msg = "forbidden", returnUrl = HttpContext.Current.Request.RawUrl})' + window.location.hash;
        break;
        case 500:
            window.location = '@Url.Action("AccessDenied", "Error")';
        break;
        case 410:
            window.location = '@Url.Action("Deleted", "Error")';
        break;
        default:
            window.location = '@Url.Action("Index", "Error")';
        break;
     }
   }
});
于 2013-04-18T11:02:38.777 回答
0

“完成”事件总是被触发。

complete: function(jqXHR, textStatus) {
    switch (jqXHR.status) {
        case 200:
            alert("error 200");
            break;
        case 404:
            alert("error 404");
            break;
        default:
            alert("DEFAULT");
    }
}
于 2013-04-18T10:58:16.057 回答