最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

angular用拦截器统一处理http请求和响应实例代码

来源:动视网 责编:小采 时间:2020-11-27 20:20:05
文档

angular用拦截器统一处理http请求和响应实例代码

angular用拦截器统一处理http请求和响应实例代码:想使用angularjs里的htpp向后台发送请求,现在有个用户唯一识别的token想要放到headers里面去,也就是{headres:{'token':1}}index.html里引入以下js:angular.module('app.factorys',[]) .factory(&#
推荐度:
导读angular用拦截器统一处理http请求和响应实例代码:想使用angularjs里的htpp向后台发送请求,现在有个用户唯一识别的token想要放到headers里面去,也就是{headres:{'token':1}}index.html里引入以下js:angular.module('app.factorys',[]) .factory(&#


想使用angularjs里的htpp向后台发送请求,现在有个用户唯一识别的token想要放到headers里面去,也就是{headres:{'token':1}}

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请求和响应实例代码

angular用拦截器统一处理http请求和响应实例代码:想使用angularjs里的htpp向后台发送请求,现在有个用户唯一识别的token想要放到headers里面去,也就是{headres:{'token':1}}index.html里引入以下js:angular.module('app.factorys',[]) .factory(&#
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top