如果将token保存在cookie,axiso请求默认是不带cookie请求,提供两种方案:
方案一:添加自定义请求头
axios.get(url, {
params: {
},
headers:{
token:TOKEN
}
})
会引发问题:在跨域时会出现options请求,解决方案后端过滤掉options请求(options请求是获取不到自定义头部信息的)
方法二:启用跨域时需要使用凭证
axios.get(url { params: { }, withCredentials:true })
会引发问题:
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:8080' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
意思就是后台设置 Access-Control-Allow-Origin 不能为* 要指定地址(http://localhost:8080);