httpProvider interceptor Test
by gkohen
HTML
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js"></script>
<div ng-app="testApp" ng-controller="testController">
{{test1}}
</div>
JavaScript
var app = angular.module('testApp', []);
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('HttpResponseErrorHandler');
}]);
app.factory('HttpResponseErrorHandler', ['$q', '$rootScope', '$timeout', function($q, $rootScope, $timeout) {
return {
response: function(response) {
alert('success: ' + response.status + ': ' + response.config.method + ': ' + response.config.url);
return response;
},
responseError: function(rejection) {
alert('rejected: ' + rejection.status + ': ' + rejection.config.method + ': ' + rejection.config.url);
return $q.reject(rejection);
}
};
}]);
app.controller('testController', ['$scope', '$http', '$timeout', function ($scope, $http, $timeout) {
$scope.test1 = 'tested';
$http.post('/echo/json', { a: true })
.success(function() {
alert('success json');
});
$http.post('/echo/FAIL', { a: true })
.success(function() {
alert('success json');
});
}]);