Angular uses the Interceptor to uniformly process http requests and responses.
If you want to use htpp in angularjs to send requests to the backend, a token that is uniquely identified by the user wants to be placed in headers, that is, {headres: {'Token': 1 }}
The following js is introduced in index.html:
angular.module('app.factorys',[]) .factory('httpInterceptor',['$q','$injector','$localStorage',function ($q,$injector,$localStorage) { var httpInterceptor = { 'responseError' : function(response) { // ...... return $q.reject(response); }, 'response' : function(response) { if (response.status == 21000) { // console.log('do something...'); } return response || $q.when(response); }, 'request' : function(config) { config.headers = config.headers || {}; if ($localStorage.token) { config.headers.token = $localStorage.token; // config.headers['X-Access-Token'] = $localStorage.token; }; return config || $q.when(config); return config; }, 'requestError' : function(config){ // ...... return $q.reject(config); } }; return httpInterceptor; }])
After the factory is injected into the app, configure it in config
.config(['$httpProvider',function(){ $httpProvider.interceptors.push(httpInterceptor);}])
The above angular method of handling http requests and responses with the interceptor is all the content that I have shared with you. I hope you can give us a reference and hope you can provide more support.