vue中request和response请求拦截器

news/2024/7/19 13:45:24 标签: vue.js, js, javascript, 前端

request 请求拦截器,有token值则配置上token值

javascript">axios.interceptors.request.use(
config=>{
   if(token && config.url != "/login"){
      config.headers['Authentication'] = token
   }
   return config;
},
error=>{
   console.log(error);
   Promise.reject(error);
}

response 服务器响应拦截器,这里拦截401错误,并重新跳入登页重新获取token

javascript">axios.interceptors.response.use(
  response=>{
    const res=response.data;
    if(res.code != 200){
      return;
    }
    return response.data;
  },
  error=>{
    if(error.response && error.response.status === 401){
      const match = /.+[#]/g.exec(location.href);
      if(process.env.NODE_ENV === 'production'){
        location.href = '' //url
      } else {
        const url = match[0] + '/login';
        location.href = url;
      }
      return false;
    }
    return Promise.reject(error.response.data);
  }
)

http://www.niftyadmin.cn/n/734086.html

相关文章

LWN: 大麻烦!Spectre继续捣乱

点击上方蓝色“Linux News搬运工”关注我们~Grand Schemozzle: Spectre continues to hauntBy Jonathan Corbet对于此前的Spectre v1硬件漏洞,大家通常的理解就是数组越界检查可能会在speculative execution(预测执行)的情况下被绕过&#xf…

js获取页面url地址

function getQueryString(key) {var reg new RegExp("(^|&)" key "([^&]*)(&|$)");var result window.location.search.substr(1).match(reg);return result ? decodeURIComponent(result[2]) : null; }

LWN: 华为EROFS文件系统

点击上方蓝色“Linux News搬运工”关注我们~erofs: promote erofs from staging从LWN上看到,华为的Gao Xiang正在向文件系统维护者申请正式把erofs从staging(候选)状态转为正式支持的文件系统。他在mailing list里面提到,EROFS的目标是希望能够在不太影响…

css实现正片叠底

mix-blend-mode: normal; //正常 mix-blend-mode: multiply; //正片叠底 mix-blend-mode: screen; //滤色 mix-blend-mode: overlay; //叠加 mix-blend-mode: darken; //变暗 mix-blend-mode: lighten; //变亮 mix-blend-mode: color-dodge; //颜色减淡 mix-blend-mode: color-…

Google正视的对手:微信小程序!

点击上方蓝色“Linux News搬运工”关注我们~今天灌个水,翻看https://developer.android.com的时候,看到页面下方弹出一个小窗口,问是否接受Google的survey,闲来无事点开填完了。其中来到下面这个问题的时候,看到微信小…

react生命周期函数渲染循序

挂载,当组件实例被创建并插入 DOM 中时,其生命周期调用顺序如下 constructor(),static getDerivedStateFromProps(不常用),render(),componentDidMount() getDerivedStateFromProps 会在调用 render 方法之前调用&…

Pandas 基础(12) - Stack 和 Unstack

这节的主题是 stack 和 unstack, 我目前还不知道专业领域是怎么翻译的, 我自己理解的意思就是"组成堆"和"解除堆". 其实, 也是对数据格式的一种转变方式, 单从字面上可能比较难理解, 所以给大家下面两张图来理解一下: 上图中, 标绿色的部分, 代表一个对应关…

LWN:对Debian的file命令增加seccomp()安全防护

点击上方蓝色“Linux News搬运工”关注我们~Hardening the "file" utility for DebianBy Jake Edge译者注:本文有删节Linux的"file"命令是一个非常应该做sandboxing(沙盒保护)的工具。它经常可能会处理不可信的输入源。不…