前端常遇到的解决跨域问题解决的尝试方案
axios.create({
withCredentials: true,
});
or axios.defaults.withCredentials = true
withCredentials: false, // default
首先将axios.defaults.withCredentials设置为true,允许请求携带Cookie;设置该属性后访问会报跨域错误,需要后端支持,
后端修改header信息
前端设置了该属性为true时,后端需要设置Access-Control-Allow-Origin为前端项目的源地址,不可设置为*(此时设置为*是不生效的,会一直报跨域);
此外还需要设置Access-Control-Allow-Creaentials为true
关于 withCredentials 属性的介绍
不同域的 XMLHttpRequest 响应可以为自己的域设置 cookie 值 不会影响到同源请求
mdn[https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/withCredentials]