我正在尝试从其他域下载静态文件。在我的.htaccess文件中,它位于根目录中:
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Accept, If-Modified-Since, Origin"
Header set Access-Control-Allow-Methods "GET, OPTIONS"
这是请求 - 响应周期,浏览器下载资源两次:
GET /file HTTP/1.1
Host: www.example.com
Accept: application/json
Origin: http://www.mydomain.com
HTTP/1.1 200 OK
Date: Sat, 07 Sep 2013 21:01:35 GMT
Server: Apache
Last-Modified: Sat, 07 Sep 2013 20:14:45 GMT
Content-Length: 2
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Accept, If-Modified-Since, Origin
Access-Control-Allow-Methods: GET, OPTIONS
Content-Type: application/json
[]
GET /file HTTP/1.1
Host: www.example.com
Cache-Control: max-age=0
Accept: application/json
Origin: http://www.mydomain.com
If-Modified-Since: Sat, 07 Sep 2013 20:14:45 GMT
HTTP/1.1 304 Not Modified
Date: Sat, 07 Sep 2013 21:01:40 GMT
Server: Apache
第二次您可以看到,由于文件尚未修改,服务器会以304 Not Modified
响应。为什么没有为第二个响应设置CORS头?
答案 0 :(得分:2)
这是一个apache bug,见下文
https://issues.apache.org/bugzilla/show_bug.cgi?id=51223
如果您感觉很勇敢,可以使用补丁重新编译Apache。