vue中request和response请求拦截器
request 请求拦截器,有token值则配置上token值axios.interceptors.request.use(config=>{if(token && config.url != "/login"){config.headers['Authentication'] = token}return config;},error=>{console.log(er
·
request 请求拦截器,有token值则配置上token值
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
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);
}
)
更多推荐
已为社区贡献3条内容
所有评论(0)