原文来自:http://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains
主要是因为涉及到CSS和JS文件是挂在别的域,然后又遇到了ajax之类的操作,所以就需要有跨域操作了,本来很方便的,只是在.htaccess文件中加了一句
Header add Access-Control-Allow-Origin "http://xxxxxx.com"
但因为备案原因,所以又添加了一个备用域名,结果,就报错了,报错提示是:
font from origin 'http://xxxxx.com' has been blocked from loading by Cross-Origin Resource Sharing policy: The 'Access-Control-Allow-Origin' header contains multiple values 'http://yyyy.com, http://zzzz.com', but only one is allowed. Origin 'http://xxxxx.com' is therefore not allowed access.
我晶,不支持多域名啊,google了一下,找到了这一篇:http://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains
不多贴,其实挺有意思。。方法也有不少
1、利用apache的环境变量:
SetEnvIf Origin "^(.*\.example\.com)$" ORIGIN_SUB_DOMAIN=$1
<FilesMatch "\.woff$">
Header set Access-Control-Allow-Origin "%{ORIGIN_SUB_DOMAIN}e" env=ORIGIN_SUB_DOMAIN
</FilesMatch>
2、用PHP来判断:
$http_origin = $_SERVER['HTTP_ORIGIN'];
if ($http_origin == "http://www.domain1.com" || $http_origin == "http://www.domain2.com" || $http_origin == "http://www.domain3.info")
{
header("Access-Control-Allow-Origin: $http_origin");
}
3、和1有点类似
SetEnvIf Origin "^http(s)?://(.+\.)?(domain\.org|domain2\.com)$" origin_is=$0
Header always set Access-Control-Allow-Origin %{origin_is}e env=origin_is
4、这是nginx的:
location /fonts {
# this will echo back the origin header
if ($http_origin ~ "example.org$") {
add_header "Access-Control-Allow-Origin" $http_origin;
}
}
更多的,还是去原网页看吧。。

评论 (1)