[ AngularJS ] Writing own interceptors

// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
return {
// optional method
'request': function(config) {
// do something on success
console.log(config.params);
console.log(config.data);
// ref : https://docs.angularjs.org/api/ng/service/$http#usage
return config;
},

// optional method
'requestError': function(rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
},

// optional method
'response': function(response) {
// do something on success
return response;
},

// optional method
'responseError': function(rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise
}
console.log(response.status); // response status code 
return $q.reject(rejection);
}
};
});

$httpProvider.interceptors.push('myHttpInterceptor');

ref : https://docs.angularjs.org/api/ng/service/$http

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s