首页 > 其他分享 >全局设置axios拦截器,请求增加username和token

全局设置axios拦截器,请求增加username和token

时间:2023-08-09 22:46:04浏览次数:46  
标签:username axios 请求 token 拦截器 config authorization

在main.js的 new vue之前加入:

axios.interceptors.request.use(
config => {
// 在发送请求前,获取新的 token
var username = window.localStorage.getItem('username');
var authorization = window.localStorage.getItem('token');
console.log(username)
console.log(authorization)
if (username && authorization) {
// 如果这两个字段存在,则添加到请求头
config.headers.authorization = authorization;
config.headers.username = username;
}

return config;
},
error => {
return Promise.reject(error);
}
);

标签:username,axios,请求,token,拦截器,config,authorization
From: https://www.cnblogs.com/lytcreate/p/17618192.html

相关文章