美文网首页
axios.interceptors.request.use(拦

axios.interceptors.request.use(拦

作者: 追风筝的Hassan | 来源:发表于2020-04-26 14:29 被阅读0次

    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.interceptors.request.use(拦

          本文链接:https://www.haomeiwen.com/subject/vundwhtx.html