服务器生成token 设置在响应头,但是前端获取不到
const token = JWT.generate({ id: new Date().getTime(), userName }, '10s')
res.header('Authorization', token)
axios.interceptors.response.use((response) => {console.log(response);if (response.data?.code == '500') {ElMessage.error(response.data.msg)router.push('/LoginView')} else {if (response.config.url == "/lanyaManage/user/login") {console.log(response.headers.authorization);sessionStorage.setItem('token', response.headers.authorization)}}return response},(error) => {console.log("请求出错:", error);}
);
其实这的原因是跨域的问题浏览器无法显示所有的响应头,需要添加设置Access-Control-Expose-Headers即可
//添加token
const token = JWT.generate({ id: new Date().getTime(), userName }, '10s')
res.header('Authorization', token)
res.setHeader("Access-Control-Expose-Headers", "Authorization")