时间:2021-07-01 10:21:17 帮助过:4人阅读
index.html里引入以下js:
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; }])
在app里注入factory后,在config里面配置
.config(['$httpProvider',function(){ $httpProvider.interceptors.push(httpInterceptor); }])
以上就是angular用拦截器统一处理http请求和响应实例代码的详细内容,更多请关注Gxl网其它相关文章!