Last active
December 16, 2015 23:49
-
-
Save benschw/5516070 to your computer and use it in GitHub Desktop.
intercepting 401 responses from $http calls in AngularJS
This file contains hidden or 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
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