我在 Rails API 上运行 Angular。对于身份验证,我正在拦截 401 状态:
.config(['$httpProvider', function($httpProvider){
// Prevent rails from making new sessions each time
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
var interceptor = ['$location', '$rootScope', '$q', function($location, $rootScope, $q) {
function success(response) {
return response;
}
function error(response) {
if (response.status === 401) {
$rootScope.$broadcast('event:unauthorized');
$location.path('/login');
return response;
}
return $q.reject(response);
}
return function(promise) {
return promise.then(success, error);
};
}];
$httpProvider.responseInterceptors.push(interceptor);
}])
Run Code Online (Sandbox Code Playgroud)
但是拦截器没有触发,因为重定向显示状态为 200
Started GET "/api/v1/stages" for 127.0.0.1 at 2014-02-23 04:38:12 +0530
Processing by StagesController#index as HTML
Completed …Run Code Online (Sandbox Code Playgroud)