拦截器——Angular

拦截器注入:

app.config(function($httpProvider) {

$httpProvider.interceptors.push('TokenInterceptor');

})

拦截器:

app.factory('TokenInterceptor', function($q,$location) {

return{

request:function(config) {

//console.log("拦截。。。。。");

//console.log(config);

config.headers['Content-Type'] = YourHeaderType;

config.headers.token = YourToken;

},

response:function(response) {

console.log(response)

return response || $q.when(response);

},

responseError:function(rejection) {

console.log(rejection)

return $q.reject(rejection);

}

};

})

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,107评论 19 139
  • 时间2017-03-31 13:43:44Hope’s Blog 原文https://blog.ygxdxx.co...
    萧玄辞阅读 15,273评论 3 16
  • 一、安装 1、 利用npm安装npm install axios --save 2、 利用bower安装bower...
    kiddings阅读 1,809评论 0 3
  • 今早我突然意识到童年已经离我远去很久了,而我总是怀念站在原地的自己,而事实上,自己已经走远了…… ...
    颜华音阅读 312评论 0 0
  • 我们很痛苦是坐在教室里做题目,本来心情就不好老师还脾气不好,教室里有个姑娘要睡着了。 她被老师发现了之后就被带到休...
    爱梦的我阅读 236评论 0 0