Before you start creating interceptors, be sure to understand the $q and deferred/promise APIs.
For purposes of global error handling, authentication, or any kind of synchronous or asynchronous pre-processing of request or postprocessing of responses, it is desirable to be able to intercept requests before they are handed to the server and responses before they are handed over to the application code that initiated these requests. The interceptors leverage the promise APIs to fulfill this need for both synchronous and asynchronous pre-processing.
The interceptors are service factories that are registered with the $httpProvider by adding them to the $httpProvider.interceptors array. The factory is called and injected with dependencies (if specified) and returns the interceptor.
There are two kinds of interceptors (and two kinds of rejection interceptors):
request
: interceptors get called with a http config object. The function is free to modify the config object or create a new one. The function needs to return the config object directly, or a promise containing the config or a new config object.requestError
: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.response
: interceptors get called with http response object. The function is free to modify the response object or create a new one. The function needs to return the response object directly, or as a promise containing the response or a new response object.responseError
: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.
// register the interceptor as a service
$provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
return {
// optional method
'request': function(config) {
// do something on success
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
}
return $q.reject(rejection);
}
};
});
$httpProvider.interceptors.push('myHttpInterceptor');
// alternatively, register the interceptor via an anonymous factory
$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
return {
'request': function(config) {
// same as above
},
'response': function(response) {
// same as above
}
};
});
平台代码
config(function ($httpProvider) {
var token = $('meta[name="csrf-token"]').attr('content'), locale = $.query('locale') || $.cookie('locale') || navigator.systemLanguage || navigator.language || 'en-US';
if (/^en/.test(locale)) {
locale = locale.split('-')[0];
}
$httpProvider.defaults.headers.common['X-CSRF-Token'] = token;
$httpProvider.defaults.headers.common['X-LANGUAGE-LOCALE'] = locale;
$.ajaxSetup({
global: true,
headers: {
'X-CSRF-Token': token,
'X-LANGUAGE-LOCALE': locale
}
});
$httpProvider.interceptors.push(function ($q) {
return {
'responseError': function (rejection) {
var status = rejection.status;
if (status == 401) {
window.location.href = '/';
} else if (status == 404) {
//$.error('');
} else if(status >= 200 && status < 300){
$.notify($.LANG(202));
} else if (status >= 400 && status < 500) {
$.error(rejection.data);
} else if (status >= 500) {
$.error($.LANG(173));
}
return $q.reject(rejection);
}
};
});
//$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
/*$httpProvider.defaults.transformRequest.unshift(function (data, headersGetter) {
return $.param(data || {});
});*/
});