zoukankan      html  css  js  c++  java
  • axios拦截器

    1.对于携带cookie,token的信息进行判断

    2.参数为接受到后台的返回数据,类似code状态值,判断后对于cookie或token进行统一处理

    axios.interceptors.request.use((config) => {
      if (Cookies.get('BJCSI_USER_LOGINKEY')) {
        const id = Cookies.get('BJCSI_USER_ID') - 0
        const loginKey = Cookies.get('BJCSI_USER_LOGINKEY')
        config.headers = {
          'Content-Type': 'application/x-www-form-urlencoded',
          'id': id,
          'loginKey': loginKey
        }
      }
      return config
    }, (err) => {
      return Promise.reject(err)
    })
    axios.interceptors.response.use(res => {
      const code = res.data.code
      if (code === 11) {
        console.log('登录过期重新登录')
        Cookies.remove('BJCSI_USER_LOGINKEY')
        Cookies.remove('BJCSI_USER_ID')
        window.location = '/'
      } else {
        return res
      }
    }, err => {
      let errResponse = err.response
      const errMsg = {'msg': code(errResponse.status)}
      if (err && errResponse) {
        return Promise.reject(errMsg)
      }
    })
  • 相关阅读:
    路径问题
    移动端推荐使用
    js获取各种宽高方法
    html 符号大全
    bzoj4923 K小值查询
    bzoj3781 小B的询问
    bzoj1799 [Ahoi2009]self 同类分布
    bzoj2005 [Noi2010]能量采集
    bzoj4039 集会
    bzoj2516 电梯
  • 原文地址:https://www.cnblogs.com/hurenjie/p/10516896.html
Copyright © 2011-2022 走看看