Skip to content

Instantly share code, notes, and snippets.

@lossendae
Created January 17, 2014 11:44
Show Gist options
  • Save lossendae/8472117 to your computer and use it in GitHub Desktop.
Save lossendae/8472117 to your computer and use it in GitHub Desktop.
AngularJS 1.2.x http Interceptor base
(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