下面是我处理全局错误的拦截器。但是我想绕过一些http请求。有什么建议吗?
var interceptor = ['$rootScope', '$q',function (scope, $q) {
function success(response) {
return response;
}
function error(response) {
var status = response.status;
if (status == 401) {
window.location = "./index.html#/404";
return;
}
if (status == 0) {
window.location = "./index.html#/nointernet";
}
return $q.reject(response);
}
return function (promise) {
return promise.then(success, error);
}
}];
$httpProvider.responseInterceptors.push(interceptor);
最佳答案
我能够简单地通过向 $http 请求的配置对象添加一个属性来实现此功能。 IE。 忽略401
。然后,在我的拦截器中,在响应错误处理程序中,检查配置对象上的属性,如果它存在,则不要转发到登录或您对 401 响应执行的任何其他操作。
首先是拦截器:
$provide.factory('authorization', function() {
return {
...
responseError: (rejection) => {
if (rejection.status === 401 && !rejection.config.ignore401) {
// redirect to login
}
return $q.reject(rejection);
}
};
});
然后,对于任何我想绕过 401 错误处理程序的请求:
$http({
method: 'GET',
url: '/example/request/to/ignore/401error',
ignore401: true
});
希望对您有所帮助。
关于javascript - AngularJs:排除来自拦截器的一些请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21036786/