奇怪的跨域访问:No 'Access-Control-Allow-Origin' header

时间:2023-03-13 09:46:20

代码是几个月前写的,之前的几个月一直运行正常。

可今天使用的时候运行失败了,提示:No 'Access-Control-Allow-Origin' header

使用 chrome、firefox 都测试了,问题一样,只能在 ie 8 上正常运行了。

在服务端,我明明添加 Access-Control-Allow-Origin: *,可就是无法跨域访问。

找了半天原因,首先在chrome中发现提交的请求中没有 cookie 部分, 我记得之前chrome浏览器默认都有提交 cookie的。

先修改客户端代码,添加:http_request.withCredentials = true; //支持跨域发送cookies

然后测试,浏览器提示 but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials.

再修改服务端,添加:Access-Control-Allow-Credentials: true

再测试,浏览器提示什么 不能使用 Access-Control-Allow-Origin: *, 必须使用 Access-Control-Allow-Origin: [http://域名]

好吧,再修改服务端代码,Access-Control-Allow-Origin: [http://域名]

再测试,问题终于解决了。

我就奇怪,之前一直运行都挺正常的,我的chrome也没升级过,怎么就突然出现这样问题了呢,请来告诉我呢?