博客
关于我
axios 全局拦截
阅读量:523 次
发布时间:2019-03-08

本文共 1628 字,大约阅读时间需要 5 分钟。

import axios from 'axios'import {    getToken }  from './auth'import {    MessageBox, Message } from 'element-ui'import store from '@/store'const service = axios.create({     baseURL: process.env.VUE_APP_BASE_API, // 接口域名地址  timeout: 5000 						// 请求超时时间})//请求拦截器(interceptors.requst)是指可以拦截每次或指定HTTP请求,并可修改配置项service.interceptors.request.use(  config => {       if (getToken()) {         config.headers['X-Token'] = getToken()// 'token'  token是在登录成功的时候才会给你 有的话加到请求头上    }      return config  },  error => {       console.log(error) // for debug    return Promise.reject(error)  })//响应拦截器(interceptors.response)可以在每次HTTP请求后拦截住每次或指定HTTP请求,并可修改返回结果项service.interceptors.response.use(  response => {       const res = response.data    if (res.code !== 20000) {         Message({           message: res.message || 'Error',        type: 'error',        duration: 5 * 1000      })      if (res.code === 50008 || res.code === 50012 || res.code === 50014) {           MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {             confirmButtonText: 'Re-Login',          cancelButtonText: 'Cancel',          type: 'warning'        }).then(() => {             store.dispatch('user/resetToken').then(() => {               location.reload()          })        })      }      return Promise.reject(new Error(res.message || 'Error'))    } else {         return res    }  },  error => {       console.log('err' + error)     Message({         message: error.message,      type: 'error',      duration: 5 * 1000    })    return Promise.reject(error)  })export default service

转载地址:http://kvanz.baihongyu.com/

你可能感兴趣的文章
Nginx + Spring Boot 实现负载均衡
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx - Header详解
查看>>
Nginx - 反向代理、负载均衡、动静分离、底层原理(案例实战分析)
查看>>
nginx 1.24.0 安装nginx最新稳定版
查看>>
nginx 301 永久重定向
查看>>
nginx css,js合并插件,淘宝nginx合并js,css插件
查看>>
Nginx gateway集群和动态网关
查看>>
Nginx Location配置总结
查看>>
Nginx log文件写入失败?log文件权限设置问题
查看>>
Nginx Lua install
查看>>
nginx net::ERR_ABORTED 403 (Forbidden)
查看>>
Nginx SSL私有证书自签,且反代80端口
查看>>
Nginx upstream性能优化
查看>>
Nginx 中解决跨域问题
查看>>
nginx 代理解决跨域
查看>>
Nginx 动静分离与负载均衡的实现
查看>>
Nginx 反向代理 MinIO 及 ruoyi-vue-pro 配置 MinIO 详解
查看>>
nginx 反向代理 转发请求时,有时好有时没反应,产生原因及解决
查看>>
Nginx 反向代理解决跨域问题
查看>>