vue-resource 拦截器(interceptor)的使用
2017-09-21 17:38
621 查看
拦截器-interceptor
在现代的一些前端框架上,拦截器基本上是很基础但很重要的一环,比如Angular原生就支持拦截器配置,VUE的Axios模块也给我们提供了拦截器配置,那么拦截器到底是什么,它有什么用? 拦截器能帮助我们解决的添加统一的request的参数 比如header中加入X-Requested-With,比如客户端需要实现sign和token的验证机制,比如你可以写$http.get('/files', params),拦截器帮你拼接成http://www.xxxx.com/1/files这样的请求地址处理统一的responseError 比如重连机制,拿到error.code错误码重连,比如token过期,重新拿到token再次send request 比如统一报错信息,给所有返回的404来个提示也会很酷
angular interceptor
angular的interceptor是个数组,在$httpProvider中进行配置,一般写成一个config,然后$httpProvider注入进来就可以配置了,把我们写的
interceptorpush进去就OK了,
$httpProvider.interceptors.push('interceptor'),interceptor一般写成一个服务
var interceptor = { 'request': function (config) { return config }, 'requestError': function (rejection) { }, 'response': function (response) { return response }, 'responseError': function (rejection) { } }return支持返回一个promise,可以对config中的params,data, url, headers等进行加工,符合我们的请求再return出去,所有的$http请求都会被拦截到
vue-resource 拦截器使用
在vue项目使用vue-resource的过程中,临时增加了一个需求,需要在任何一个页面任何一次http请求,增加对token过期的判断,如果token已过期,需要跳转至登录页面。如果要在每个页面中的http请求操作中添加一次判断,那么会是一个非常大的修改工作量。那么vue-resource是否存在一个对于任何一次请求响应捕获的的公共回调函数呢?答案是有的!vue-resource的interceptors拦截器的作用正是解决此需求的妙方。在每次http的请求响应之后,如果设置了拦截器如下,会优先执行拦截器函数,获取响应体,然后才会决定是否把response返回给 then进行接收。那么我们可以在这个拦截器里边添加对响应状态码的判断,来决定是跳转到登录页面还是留在当前页面继续获取数据。拦截器详细介绍》》
下边代码添加在main.js中Vue.http.interceptors.push((request, next) => { console.log(this)//此处this为请求所在页面的Vue实例// modify requestrequest.method = 'POST';//在请求之前可以进行一些预处理和配置// continue to next interceptor next((response) => {//在响应之后传给then之前对response进行修改和逻辑判断。对于token时候已过期的判断,就添加在此处,页面中任何一次http请求都会先调用此处方法 response.body = '...'; return response;});});实例:
/******************拦截器设置请参考这部分(开始)******************/Vue.http.interceptors.push((request, next) =>{//登录成功后将后台返回的TOKEN在本地存下来,每次请求从sessionStorage中拿到存储的TOKEN值let TOKEN=sessionStorage.getItem('STORAGE_TOKEN');if(TOKEN){//如果请求时TOKEN存在,就为每次请求的headers中设置好TOKEN,后台根据headers中的TOKEN判断是否放行request.headers.set('TOKEN',TOKEN);}next((response) => {return response;});});/******************拦截器设置请参考这部分(结束)******************/
//拦截器Vue.http.interceptors.push((request, next) => {// console.log(Login.item);var tokens = localStorage.getItem('token');request.headers.set('Authorization', tokens);// console.log(request.headers);help.showLoading = true;next((response) => {// console.log(response);help.showLoading = false;return response})})
相关文章推荐
- vue-resource 拦截器(interceptor)的使用
- vue-resource 拦截器(interceptor)的使用详解
- vue-resource 拦截器使用
- 关于使用vue-resource中使用interceptor涉及jwt_token的错误问题
- Vue添加请求拦截器及vue-resource 拦截器使用
- vue-resource 拦截器使用详解
- 在struts2中使用拦截器(Interceptor)控制登录和权限
- vue-resource -- 全局拦截器interceptors
- struts2使用Interceptor接口来实现拦截器
- vue cli+axios踩坑记录+拦截器使用,代理跨域proxy(更新)
- (二)vue开发 - 使用vue-resource实现网络请求
- Spring MVC 中 HandlerInterceptorAdapter的使用(拦截器)
- Vue.js——使用$.ajax和vue-resource实现OAuth的注册、登录、注销和API调用
- 简单了解如何使用vue-router和vue-resource
- 当我们使用OkHttpClient 的时候,增加自定InterCeptor 拦截器的时候需要注意
- CXF拦截器(Interceptor)的使用
- 使用Unity extension 设置默认的拦截器(interceptor)
- 在struts2中使用拦截器(Interceptor)控制登录和权限
- retrofit2.0使用拦截器Interceptor统一打印请求与响应的json
- 在struts2中使用拦截器(Interceptor)控制登录和权限