Skip to content

Instantly share code, notes, and snippets.

@benschw
Last active December 16, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benschw/5516070 to your computer and use it in GitHub Desktop.
Save benschw/5516070 to your computer and use it in GitHub Desktop.
intercepting 401 responses from $http calls in AngularJS
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives'], function ($routeProvider, $locationProvider, $httpProvider) {
var interceptor = ['$rootScope', '$q', function (scope, $q) {
function success(response) {
return response;
}
function error(response) {
var status = response.status;
if (status == 401) {
window.location = "./index.html";
return;
}
// otherwise
return $q.reject(response); //similar to throw response;
}
return function (promise) {
return promise.then(success, error);
}
}];
$httpProvider.responseInterceptors.push(interceptor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment