时间:2020-05-08 22:09:12 | 栏目: | 点击:次
axios.defaults.withCredentials = true然后跨域请求时会出现如下问题:
Response to preflight request doesn't pass access control check: 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 字段必须指定域名,不能为* Access-Control-Allow-Credentials为true
Access-Control-Allow-Credentials: true如果发送的是带凭据的请求,但服务器的相应中没有包含这个头部,那么浏览器就不会把相应交给JavaScript,请求就无法得到结果的数据(浏览器得到了,但是我们请求的方法得不到,因为被浏览器拦截了),因此在需要传Cookie等时,服务端的Access-Control-Allow-Origin必须配置具体的具体的域名。并且还需要设置其他的请求头:
header('Access-Control-Allow-Origin:http://www.xxx.com');
header('Access-Control-Allow-Credentials: true'); //是否支持cookie跨域
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");