关于javascript:IE没有处理401错误

IE is not handleing 401 error

我有一个 ajax 调用,部分用于检查您是否已登录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function userInfo(){
    return $.ajax({
        url:"http://api.itassistteam.com/api/account/isloggedon",
        type:"GET",
        dataType:"json",
        xhrFields: {
            withCredentials: true
        },
        statusCode: {
            401: function(){
                console.log("401 error.");
                deleteCookie();
                window.location.href="./login.html";
            }
        },
    });
}

在 Chrome、FF 和 Opera 中,它会像预期的那样重定向到登录页面,但在 IE 中它什么也不做。当我查看控制台时,它显示

1
SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.

知道为什么 IE 不接受错误处理程序并重定向到登录吗?"401 错误"消息未出现在控制台中。


我使用了错误而不是 statusCode。这就是您如何获得与

相同的错误结果

1
2
3
statusCode : {
    401: function(){...}
}

你会的,

1
2
3
4
5
error: function(jqXHR, textStatus, errorThrown){
    if(jqXHR.status == 401){
        ...
    }
}

这在所有主要浏览器中都是一致的,不像 statusCode 方法女巫不适用于 IE。这样您就可以针对 IE 中的错误进行错误处理。