Created
April 30, 2014 11:17
-
-
Save jianbo/5139946b9860e8f11846 to your computer and use it in GitHub Desktop.
interceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.config([ | |
'$httpProvider', function($httpProvider, $injector) { | |
var interceptor; | |
interceptor = [ | |
'$injector', '$rootScope', '$q', function($injector, $rootScope, $q) { | |
var error, success; | |
success = function(response) { | |
// console.log(response); | |
return response; | |
}; | |
error = function(response) { | |
console.log(response.status); | |
if (response.status === 401) { | |
$rootScope.$broadcast('event:unauthorized'); | |
$injector.invoke(function($http, $state) { | |
$state.go('authentication.login'); | |
}); | |
return response; | |
} | |
return $q.reject(response); | |
}; | |
return function(promise) { | |
return promise.then(success, error); | |
}; | |
} | |
]; | |
return $httpProvider.responseInterceptors.push(interceptor); | |
} | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment