axios.interceptors.request.use(拦截器)

axios.interceptors.request.use(拦截器)

//请求
axios.interceptors.request.use(
  function(config) {
    if (config.method === 'get' && config.url != undefined) {
      if (config.params == undefined) {
        config.params = {}
      }
      config.params.__preventCache = new Date().getTime()
    }
    config.withCredentials = true // 允许携带token ,这个是解决跨域产生的相关问题
    return config
  },
  function(error) {
    return Promise.reject(error)
  }
)
//返回状态判断
axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if (error.response.status == '401') {
      if (process.env.NODE_ENV === 'local') {
        window.location.href = '/#/login'
      } else {
        window.location.href =
          loginUrl + '?app=7&redirect=' + encodeURIComponent(document.URL)
      }
    } else if (error.response.status == '400') {
      ElementUI.Message.error(error.response.data.message)
    } else {
      ElementUI.Message.error(error.response.data.message)
    }
    return Promise.reject(error)
  }
)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • axios 是一个基于 Promise 的http请求库,可以用在浏览器和node.js中 备注: 每一小节都会从...
    Polaris_ecf9阅读 685评论 0 1
  • Vue项目越做越多,Axios一直作为请求发送的基础工程,这里就深究一下Axios的拦截器相关的一些逻辑和对应一个...
    RandyZhang阅读 32,670评论 9 31
  • 页面发送http请求,很多情况我们要对请求和其响应进行特定的处理;如果请求数非常多,单独对每一个请求进行处理会变得...
    岁末Zzz阅读 1,972评论 0 0
  • axios 基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用 功能特性 在...
    Yanghc阅读 3,710评论 0 7
  • Axios是一个基于Promise的HTTP请求库,可以用在浏览器和Node.js中。平时在Vue项目中,经常使用...
    多啦斯基周阅读 889评论 0 0