1

I use AngularJS for my application and ui-route. A service in my application looks like this:

(function() {
'use strict';

angular
    .module('myProject.myModule')
    .factory('myService', myService);

myService.$inject = ['$http', 'api_config'];

function myService($http, api_config) {
    var service = {
            myServiceMethod1: myServiceMethod1,
            ...
    };

    return service;

    ////////////

    function myServiceMethod1(params) {
        return $http.get(api_config.BASE_URL + '/path');
    }

Now I will implement an (global) intercetor in that way that any time a response status is HTTP 403 the interceptor should handle it. This interceptor should be globally. Thanks a lot!

1 Answer 1

3

Try something like

angular
    .module('myProject.myModule')
    .config(['$httpProvider', function ($httpProvider) {

 $httpProvider.interceptors.push(['$q', '$location', function ($q, $location) {
        return {
            'responseError': function(response) {
                if(response.status === 401 || response.status === 403) {
                    $location.path('/signin'); // Replace with whatever should happen
                }
                return $q.reject(response);
            }
        };
    }]);
}]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.