AngularJs:从Interceptor中排除一些请求

Gov*_*vil 13 javascript angularjs

下面是我的拦截器,它处理全局错误.但我想绕过一些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);
Run Code Online (Sandbox Code Playgroud)

aar*_*onp 31

我只需在$ http请求的config对象中添加一个属性即可实现此功能.即.ignore401.然后,在我的拦截器中,在响应错误处理程序中,检查配置对象上的属性,如果它存在,则不要转发到登录或您在401响应上执行的任何其他操作.

首先,拦截器:

$provide.factory('authorization', function() {
    return {
        ...

        responseError: (rejection) => {
            if (rejection.status === 401 && !rejection.config.ignore401) {
                // redirect to login
            }

            return $q.reject(rejection);
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

然后,对于我想绕过401错误处理程序的任何请求:

$http({
    method: 'GET',
    url: '/example/request/to/ignore/401error',
    ignore401: true
});
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.