【发布时间】:2017-01-07 02:11:20
【问题描述】:
我为我的静态子域配置了 CORS,我可以在我的主域和子域中使用静态文件。
问题是我在 javascript 文件中有一个 get 请求,而 firefox 只针对该请求抱怨,即与我在 html 代码上发出的其余请求相同的静态子域。
我不记得以前有过这个问题,也许 CORS (firefox) 中的一些更改使它不再工作?
脚本(来自 .js 文件内部):
$('#example').DataTable( {
//[...]
language: {
"url": "//static.domain.com/js/DataTables-Spanish.json"
},
//[...]
} );
这是它实际做的,一个jquery ajax请求方法:
if ( oLanguage.sUrl !== "" )
{
/* Get the language definitions from a file - because this Ajax call makes the language
* get async to the remainder of this function we use bInitHandedOff to indicate that
* _fnInitialise will be fired by the returned Ajax handler, rather than the constructor
*/
$.ajax( {
dataType: 'json',
url: oLanguage.sUrl,
success: function ( json ) {
_fnLanguageCompat( json );
_fnCamelToHungarian( defaults.oLanguage, json );
$.extend( true, oLanguage, json );
_fnInitialise( oSettings );
},
error: function () {
// Error occurred loading language file, continue on as best we can
_fnInitialise( oSettings );
}
} );
bInitHandedOff = true;
}
firefox控制台报错:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://static.mydomain.com/js/DataTables-Spanish.json. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
CORS 配置(在 vhost 块内):
<IfModule mod_headers.c>
SetEnvIfNoCase Origin: "https?://(www\.)?(domain\.com|sub1\.domain\.com)(:\d+)?$" ACAO=$0
Header set Access-Control-Allow-Origin: "%{ACAO}e" env=ACAO
Header set Access-Control-Allow-Methods: "GET"
</IfModule>
再一次,正如您所见,CORS 在静态子域中已正确配置,我只收到关于 javascript 文件内的请求的抱怨。
为什么 Firefox 会抱怨?
火狐网络监控器
Status: 200 - Method: GET - File: DataTables-Spanish.json - Domain: (green lock) static.domain.com - Cause: script - Type: json
-Request headers:
Host: static.domain.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en,en-US;q=0.7,es;q=0.3
Accept-Encoding: gzip, deflate, br
Referer: https://sub1.domain.com/site
Origin: https://sub1.domain.com
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
-Response headers:
Accept-Ranges: bytes
Content-Length: 932
Content-Type: application/json
Date: Fri, 06 Jan 2017 02:34:49 GMT
Etag: "3a4-52ef9645b6940"
Last-Modified: Sat, 26 Mar 2016 20:19:09 GMT
Server: Apache/2.4.25 (Unix) OpenSSL/1.0.2j
X-Firefox-Spdy: h2
access-control-allow-methods: GET
strict-transport-security: max-age=63072000; includeSubdomains;
编辑:如果我在配置中使用通配符而不是正则表达式,它可以正常工作:
<IfModule mod_headers.c>
#SetEnvIfNoCase Origin: "https?://(www\.)?(domain\.com|sub1\.domain\.com)(:\d+)?$" ACAO=$0
#Header set Access-Control-Allow-Origin: "%{ACAO}e" env=ACAO
Header set Access-Control-Allow-Origin: *
Header set Access-Control-Allow-Methods: "GET"
</IfModule>
所以现在的问题是:为什么它不能使用正则表达式?
如果我将标题放在最后一行,它将与正则表达式一起使用:
<IfModule mod_headers.c>
SetEnvIf Origin "https://(www.domain.com|sub1.domain.com|auth.domain.com)$" ACAO=$0
Header set Access-Control-Allow-Methods "GET"
Header always append Access-Control-Allow-Origin "%{ACAO}e" env=ACAO
</IfModule>
【问题讨论】:
-
如果直接在浏览器中调用DataTables-Spanish.json文件,CORS头是否存在?
-
当我做 ctrl+F5 是的。我在想,也许这个函数没有get方法?
-
好的,现在再次尝试您的代码……现在可以工作了吗?也许你只是在缓存中有一些旧的标头。
-
不,它一直在抱怨,这真的很奇怪,因为我可以在它抱怨的页面中看到标题 (
access-control-allow-methods:"GET")。 -
抱歉造成混淆,我说“当我执行 ctrl+F5 时”,因为当状态码为 304 未修改时,不会再次收到 cors 标头(对于所有文件,而不仅仅是 json) .