Capture HTTP 401 with Angular.js interceptor

前端 未结 4 1732
暗喜
暗喜 2020-11-27 17:00

I\'d like to implement authentication on a single page web app with Angular.js. The official Angular documentation recommends the using of interceptors:

$pro         


        
4条回答
  •  眼角桃花
    2020-11-27 17:37

    I don't know why, but response with 401 error goes into success function.

    'responseError': function(rejection)
                    {
                        // do something on error
    
                        if (rejection.status == 401)
                        {
                            $rootScope.signOut();
                        }
    
                        return $q.reject(rejection);
                    },
                    'response': function (response) {
                        // do something on error
                        if (response.status == 401) {
                            $rootScope.signOut();
                        };
                        return response || $q.when(response);
                    }
    

提交回复
热议问题