应用场景:

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. C++官方文档-静态成员

    #include <iostream> using namespace std; class Dummy { public: static int n; int x; Dummy() : ...

  2. 《GPU高性能编程CUDA实战》附录一 高级原子操作

    ▶ 本章介绍了手动实现原子操作.重构了第五章向量点积的过程.核心是通过定义结构Lock及其运算,实现锁定,读写,解锁的过程. ● 章节代码 #include <stdio.h> #incl ...

  3. rabbitMQ 的基本知识

    参考:  https://www.cnblogs.com/dwlsxj/p/RabbitMQ.html

  4. JMH使用说明

    JMH使用说明 一.概述 JMH,即Java Microbenchmark Harness,是专门用于代码微基准测试的工具套件.何谓Micro Benchmark呢?简单的来说就是基于方法层面的基准测 ...

  5. 创建类type (底层代码)

    类的创建时是用type 实现的 def __init__(self, name): self.name = name def fun(self): print("%s is talking' ...

  6. javascript时间日期操作

    Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();   ...

  7. unity WWW加载进度条

    using UnityEngine; using System.Collections; public class testProgress : MonoBehaviour { void Start ...

  8. How to Pronounce EVERY

    How to Pronounce EVERY Share Tweet Share Tagged With: 2-Syllable Everybody should learn the word ‘ev ...

  9. maven配置国内镜像库

    https://www.cnblogs.com/xiongxx/p/6057558.html

  10. node连续查询两次数据库返回方式(文档未定)

    function db(callback){ var mysql = require('mysql'); var connection = mysql.createConnection({ host ...