应用场景:

1,每个请求都带上的参数,比如token,时间戳等。

2,对返回的状态进行判断,比如token是否过期

代码如下:

[javascript] view plain copy

  1. axios.interceptors.request.use( 
  2. config => { 
  3. var xtoken = getXtoken() 
  4. if(xtoken != null){ 
  5. config.headers['X-Token'] = xtoken 
  6. if(config.method=='post'){ 
  7. config.data = { 
  8. ...config.data, 
  9. _t: Date.parse(new Date())/1000, 
  10. }else if(config.method=='get'){ 
  11. config.params = { 
  12. _t: Date.parse(new Date())/1000, 
  13. ...config.params 
  14. return config 
  15. },function(error){ 
  16. return Promise.reject(error) 
  17. axios.interceptors.response.use(function (response) { 
  18. // token 已过期,重定向到登录页面
  19. if (response.data.code == 4){ 
  20. localStorage.clear() 
  21. router.replace({ 
  22. path: '/signin', 
  23. query: {redirect: router.currentRoute.fullPath} 
  24. }) 
  25. return response 
  26. }, function (error) { 
  27. // Do something with response error
  28. return Promise.reject(error) 
  29. })

axios 设置拦截器 全局设置带默认参数(发送 token 等)的更多相关文章

  1. axios请求,拦截器的使用

    1. axios 创建请求 import axios from 'axios' import {Message} from 'element-ui' import router from " ...

  2. axios的拦截器(Interceptors)

    axios 的拦截器:interceptors 如果我们想在请求之前做点什么,用拦截器再好不过了 拦截器一般做什么? 1. 修改请求头的一些配置项 2. 给请求的过程添加一些请求的图标 3. 给请求添 ...

  3. axios 基于拦截器的取消(重复)请求

    axios 基于拦截器的取消(重复)请求 // 添加请求拦截器 axios.interceptors.request.use((config) => { // 准备发请求之前, 取消未完成的请求 ...

  4. 【c++】类中带默认参数的函数

    反思两个问题 1. 带默认参数的函数,为何声明.定义不能同时有参数? 2. 带默认参数的函数, 为何带默认参数的参数靠后站? 上程序 #include <iostream> #includ ...

  5. php课程 3-12 带默认参数的函数怎么写

    php课程 3-12 带默认参数的函数怎么写 一.总结 一句话总结:多看学习视频啊 1.带默认参数的函数怎么写? 直接等于号接默认参数,和js里面一模一样 2.带默认参数的函数的注意事项是什么? 默认 ...

  6. js课程 2-7 带默认参数的函数怎么写

    js课程 2-7 带默认参数的函数怎么写(注意参数顺序) 一.总结 一句话总结:默认参数一定要放在最后面,而且还有注意你调用参数的时候给参数的顺序习惯.直接加个等于号就可以是默认参数.function ...

  7. (vue.js)axios interceptors 拦截器中添加headers 属性

    (vue.js)axios interceptors 拦截器中添加headers 属性:http://www.codes51.com/itwd/4282111.html 问题: (vue.js)axi ...

  8. SpringBoot 拦截器获取http请求参数

    SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 目录 SpringBoot 拦截器获取http请求参数-- 所有骚操作基础 获取http请求参数是一种刚需 定义拦截器获取请求 为 ...

  9. 如何实现 Https拦截进行 非常规“抓包” 珍惜Any 看雪学院 今天 前段时间在自己做开发的时候发现一个很好用的工具,OKHttp的拦截器(何为拦截器?就是在每次发送网络请求的时候都会走的一个回调)大概效果如下:

    如何实现 Https拦截进行 非常规“抓包” 珍惜Any 看雪学院 今天 前段时间在自己做开发的时候发现一个很好用的工具,OKHttp的拦截器(何为拦截器?就是在每次发送网络请求的时候都会走的一个回调 ...

随机推荐

  1. Python常量工具类

    1.定义常量类constant.py # -*- coding: utf-8 -* """常量工具类 author: Jill usage: from constant ...

  2. python入门-WHILE循环

    1 使用while循环 current_number= : print(current_number) current_number += 2 让用户选择退出 prompt = "tell ...

  3. 温故而知新-MySQL高级编程

    1 load data infile语句 MySQL下的命令  登录mysql命令行模式 load data infile "/var/www/1.txt" into table ...

  4. Redis hash数据结构

    1, 新增一个 hash 或者 新增数据 => hset key field value 2, 获取某个字段值 => hset key field 3, 获取所有字段值 => hge ...

  5. left join 如何增加where条件(在on的后面),这很重要

    SELECT [学号], [姓名],[备注2],[年级],专业,[学院],[x30] FROM [总表] left join k指标体系  on  学号 = x01 where 年级='2014'

  6. leetcode937

    public class Solution { public string[] ReorderLogFiles(string[] logs) { var list1 = new List<str ...

  7. leetcode263

    public class Solution { private bool Judge(int x) { ) { return false; } int bound = Convert.ToInt32( ...

  8. iOS 真机调试 Xcode 显示 device Error: device unavailable

    一般来说有两个原因: 1. iphone没有加到test device里,将iphone的设备id加到test device列表里 2. Xcode不支持当前的iOS版本,将Xcode升级到最新版

  9. Haskell语言学习笔记(25)MonadState, State, StateT

    MonadState 类型类 class Monad m => MonadState s m | m -> s where get :: m s get = state (\s -> ...

  10. 使用CXF发布的WebService报错:org.apache.cxf.interceptor.Fault: The given SOAPAction does not match an operation

    场景:JAVA语言使用CXF搭建WebService发布报错 错误信息:org.apache.cxf.interceptor.Fault: The given SOAPAction does not ...