Capture HTTP 401 with Angular.js interceptor

前端 未结 4 1745
暗喜
暗喜 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:27

    For AngularJS >1.3 use $httpProvider.interceptors.push('myHttpInterceptor');

    .service('authInterceptor', function($q) {
        var service = this;
    
        service.responseError = function(response) {
            if (response.status == 401){
                window.location = "/login";
            }
            return $q.reject(response);
        };
    })
    .config(['$httpProvider', function($httpProvider) {
        $httpProvider.interceptors.push('authInterceptor');
    }])
    

提交回复
热议问题