AngularJs: Exclude some requests from Interceptor

angularjs, javascript

Solution

I was able to implement this functionality simply by adding a property to the config object of the $http request. ie. `ignore401`. Then, in my interceptor, in the response error handler, check for the property on the config object, and if it is present, do not forward to login or whatever else you do on a 401 response.

First, the interceptor:

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

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

            return $q.reject(rejection);
        }
    };
});

Then, for any request that I want to bypass the 401 error handler:

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

Hope that helps.

Problem

Below is my interceptor which handles global errors. But I want to bypass some http requests. Any suggestions ? ``` 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); ```

Original source