Created
January 17, 2014 11:44
-
-
Save lossendae/8472117 to your computer and use it in GitHub Desktop.
AngularJS 1.2.x http Interceptor base
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
(function (window, angular, undefined) { | |
'use strict'; | |
var myAppInterceptor = function ($httpProvider) { | |
var interceptor = ['$q' function ($q) { | |
return { | |
return { | |
// On request success | |
request: function (config) { | |
// Contains the data about the request before it is sent. | |
// console.log(config); | |
// Return the config or wrap it in a promise if blank. | |
return config || $q.when(config); | |
}, | |
// On request failure | |
requestError: function (rejection) { | |
// Contains the data about the error on the request. | |
// console.log(rejection); | |
// Return the promise rejection. | |
return $q.reject(rejection); | |
}, | |
// On response success | |
response: function (response) { | |
// Contains the data from the response. | |
// console.log(response); | |
// Return the response or promise. | |
return response || $q.when(response); | |
}, | |
// On response failture | |
responseError: function (rejection) { | |
// Contains the data about the error. | |
// console.log('responseError', rejection); | |
// Return the promise rejection. | |
return $q.reject(rejection); | |
} | |
}; | |
}]; | |
$httpProvider.interceptors.push(nsInterceptor); | |
}; | |
angular.module('myApp', []) | |
.config(['myAppInterceptor', myAppInterceptor]); | |
})(window, window.angular); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment