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'.
配合前端写Vue项目时,浏览器遇到这种错误,按道理来说http response 的 header 中 Access-Control-Allow-Origin参数 为* 时,可以满足一切域名情况。
看样子 看了半天也不行,修改nginx的配置 Access-Control-Allow-Origin 改为 http://xxx.xxx.com时候,可以了。最后查找跨域文档才发现
跨域请求发送cookie的时候,需要判断是否包含了请求源的origin,不然不发送cookie,然后,OPTIONS请求发送成功后,并没有新的请求发出。
解决方法是:
nginx配置修改一下
add_header Access-Control-Allow-Origin *;
改成
add_header Access-Control-Allow-Origin "$http_origin";
这样满足任何情况
附上完整的跨域配置
add_header 'Access-Control-Allow-Origin' $http_origin always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}