0

从请求到服务器,我返回 401 错误。

我想处理这个并适当地重定向用户。

起初我尝试使用 ajaxSetup 来捕获错误并处理它,但这并没有受到影响。

然后我将其更改为使用 ajaxError 方法,这有效。

为什么我使用 ajaxSetup 时没有命中错误方法?

这有效:

$("body").ajaxError(
                function (e, request) {
                    if (request.status == 401) {
                        alert("Your session has expired. Please login again to continue.");
                        window.location = "/admin/account/logon";
                    }
                }
            );

但这不起作用:

$.ajaxSetup({
        cache: false,
        error: function (jqXHR, textStatus, errorThrow) {
            if (jqXHR.status == 401) {
                alert("Your session has expired. Please login again to continue.");
                window.location = "/admin/account/logon";
            }
        }
    });

它是使用 Jquery Unobtrusive Ajax 触发的,链接如下所示:

<a title="Delete this comment" class="close " data-ajax="true" data-ajax-confirm="Are you sure you want to delete this comment?" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#Comments" href="/myurl">×</a>
4

1 回答 1

3

从文档中

Note: 
Global callback functions should be set with their respective global Ajax event handler methods—
.ajaxStart(),
 .ajaxStop(),
 .ajaxComplete(),
 .ajaxError(),
 .ajaxSuccess(),
 .ajaxSend()

—rather than within the options object for $.ajaxSetup().
于 2013-03-15T04:35:29.927 回答