问题描述
我想要跨域带上cookies
,为什么withCredentials: true
不起作用?
问题出现的环境背景及自己尝试过哪些方法
我尝试过axios.defaults.withCredentials = true
可以起作用。
但是为什么单独配置,没有作用?
相关代码
axios.post('http://101.132.138.141:8888/service/pageUsers', objectToForm({
'currentPage': '1',
'pageSize': '10',
'token': '7e987daa-6c84-46d2-be26-f345dfaed8a7',
}), {
// 单独配置
withCredentials: true
})
.then(function(res) {
console.log(res.data);
})
.catch(function(err) {
console.error(err);
});
实际看到的错误信息又是什么?
已拦截跨源请求:同源策略禁止读取位于 http://101.132.138.141:8888/service/pageUsers 的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-Origin')。
后端已经设置了CORS
头,但是因为没有附带上cookies
,所以被拦截器拦截了。
withCredentials的情况下,后端要设置Access-Control-Allow-Origin为你的源地址,例如http://localhost:8080,不能是*,而且还要设置header('Access-Control-Allow-Credentials: true');