htaccess 访问控制允许来源 [英] htaccess Access-Control-Allow-Origin

查看:29
本文介绍了htaccess 访问控制允许来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个可在其他网站上从外部加载的脚本.它加载 CSS 和 HTML 并且在我自己的服务器上运行良好.

I'm creating a script that loads externally on other sites. It loads CSS and HTML and works fine on my own servers.

然而,当我在另一个网站上尝试时,它显示了这个可怕的错误:

However, when I try it on another website it displays this awful error:

Access-Control-Allow-Origin

在这里你可以看到它完美加载:http://tzook.info/bot/

Here you can see it loads perfectly: http://tzook.info/bot/

但是在另一个网站上,它显示了错误:http://cantloseweight.co/robot/

But on this other website it shows the error: http://cantloseweight.co/robot/

我将加载脚本上传到 jsfiddle:http://jsfiddle.net/TL5LK/

I uploaded the loading script to jsfiddle: http://jsfiddle.net/TL5LK/

我尝试像这样编辑 htaccess 文件:

I tried editing the htaccess file like this:

<IfModule mod_headers.c>    
    Header set Access-Control-Allow-Origin *
</IfModule>

或者像这样:

Header set Access-Control-Allow-Origin *

但是还是不行.

推荐答案

在外部根文件夹的.htaccess中试试这个:

Try this in the .htaccess of the external root folder :

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

如果它只涉及 .js 脚本,你应该把上面的代码包装在这个里面:

And if it only concerns .js scripts you should wrap the above code inside this:

<FilesMatch ".(js)$">
...
</FilesMatch>

这篇关于htaccess 访问控制允许来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆