Igor Simic
7 years ago

intercept header request and modify some data


intercept request / response and get / modify some data (header status number ...)



app.factory('resourceInterceptor', ['$q','$localStorage','$location','loginService', function ($q,$localStorage,$location,loginService) {
    return {
        request: function (config) {
            config.headers = config.headers || {};
            // insert code to populate your request header for instance
             
             
            // add token
            var obj = {Token: $localStorage.cdttkn};
            Object.assign(config.headers, obj);

            //console.log("request sent!");
            //console.log(config.headers);
            

            return config;
        },
        response: function (response) {
           
          
            //console.log(response.headers() );
            //console.log(response);
             
            return response || $q.when(response);
        },

        responseError: function(rejection) {
                //console.log("bad response");
                //console.log(rejection);

                if ( rejection.status === 406) {
             
                    //406 Not Acceptable
                    //console.log(response);
              
                   // logout user and redirect
                    loginService.logout();
                    $location.path('/');

                    alert("stop logout");

                }

                if ( rejection.status === 403) {
                 
                    //403 Forbidden you don't have necessary permissions for the resource
                    //console.log(response);

                    //just redirect
                    alert("Not enpugh rights for this resouurce!")

                }

                if(rejection.config.handleError && rejection.status === 403){
                    //show error dialog
                     console.log("4033 - 2");
                    
                }
               
                return rejection;
            }
    };
}]);



http://stackoverflow.com/questions/25366864/catching-403-errors-in-angular-and-showing-a-pop-up,