IE10将状态代码401/403解释为网络错误

时间:2015-01-23 19:25:13

标签: angularjs internet-explorer internet-explorer-10

我有一个AngularJS responseError拦截器,用于广播401和403 HTTP状态代码的事件。但IE10(according to this)中有一个错误,当它从服务器(未登录)或403(未授权)获取401状态代码时,IE将其解释为网络错误,因此{{1}拦截器永远不会被触发,因此用户永远不会被重定向到登录页面。

代码:

responseError

这是一个截图:

http://i.imgur.com/8iZQjFM.png

有没有解决方案?

编辑: app.factory('authHttpInterceptor', ["$rootScope", "AUTH_EVENTS", "$q", function ($rootScope, AUTH_EVENTS, $q) { return { 'request': function (config) { return config; }, 'requestError': function (rejection) { return $q.reject(rejection); }, 'response': function (response) { return response; }, 'responseError': function (rejection) { $rootScope.$broadcast({ 401: AUTH_EVENTS.notAuthenticated, 403: AUTH_EVENTS.notAuthorized }[rejection.status], rejection); return $q.reject(rejection); } }; }]); 拦截器被解雇了,如果状态为401或403,我会检查那里,然后从那里广播它,但它只会被解雇,如果响应是成功的(200 OK)。

编辑2:事实证明response拦截器被调用,但状态代码为responseError

编辑3:我通过添加0来解决此问题,因为如果我们收到HTTP状态代码0,那么显然有些问题。

1 个答案:

答案 0 :(得分:6)

这是bug in Internet Explorer 10,由Microsoft关闭,因为不可复制。该错误允许IE将来自服务器的401状态代码解释为网络错误,因此无法确定用户是否未经授权或仅仅丢失了互联网连接。

我通过将0添加为错误事件来解决此问题,因此在收到状态代码0时,它会广播notAuthenticated事件。根据{{​​3}},没有状态代码0因此在收到时会出现问题。

我将拦截器更改为以下内容:

'responseError': function (rejection) {
    $rootScope.$broadcast({
        0: AUTH_EVENTS.notAuthenticated, // Edge case, if status code from server is 0, clearly something is wrong
        401: AUTH_EVENTS.notAuthenticated,
        403: AUTH_EVENTS.notAuthorized
    }[rejection.status], rejection);
    return $q.reject(rejection);
}