Want to use the http in angularjs to send a request to the background. Now there is a token uniquely identified by the user who wants to put it in the headers, that is {headres:{'token':1}}
The following js are introduced:
('',[]) .factory('httpInterceptor',['$q','$injector','$localStorage',function ($q,$injector,$localStorage) { var httpInterceptor = { 'responseError' : function(response) { // ...... return $(response); }, 'response' : function(response) { if ( == 21000) { // ('do something...'); } return response || $(response); }, 'request' : function(config) { = || {}; if ($) { = $; // ['X-Access-Token'] = $; }; return config || $(config); return config; }, 'requestError' : function(config){ // ...... return $(config); } }; return httpInterceptor; }])
After injecting factory in the app, configure it in config
.config(['$httpProvider',function(){ $(httpInterceptor); }])
The above method of using an interceptor to uniformly handle http requests and responses is all the content I share with you. I hope you can give you a reference and I hope you support me more.