如何添加 Access-Control-Allow-Origin 标头
我正在设计一个网站(例如 mywebsite.example
),该网站从另一个网站(例如 anothersite.example
)加载 font-face 字体。我在 Firefox 中加载字体时遇到问题,我阅读了 此博客:
Firefox(支持@font-face 从v3.5开始)不允许跨域 默认字体。这意味着字体 必须从同一域提供 (和子域)除非您可以添加 “Access-Control-Allow-Origin”标头 到字体。
如何将 Access-Control-Allow-Origin 标头设置为字体?
I am designing a website (e.g. mywebsite.example
) and this site loads font-face fonts from another site (say anothersite.example
). I was having problems with the font face font loading in Firefox and I read on this blog:
Firefox (which supports @font-face
from v3.5) does not allow cross-domain
fonts by default. This means the font
must be served up from the same domain
(and sub-domain) unless you can add an
“Access-Control-Allow-Origin” header
to the font.
How can I set the Access-Control-Allow-Origin header to the font?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
所以你要做的是......在字体文件文件夹中放置一个 htaccess 文件,其中包含以下内容。
同样在远程CSS文件中,font-face声明需要字体文件的完整绝对URL(本地CSS文件中不需要):
例如,
这将解决问题。需要注意的一件事是,您可以准确指定应允许哪些域访问您的字体。在上面的 htaccess 中,我已指定每个人都可以使用
"*"
访问我的字体,但是您可以将其限制为:单个 URL:
标头设置 Access-Control-Allow-Origin http://example.com
或以逗号分隔的 URL 列表Access-Control-Allow -Origin: http://site1.com,http://site2.com
(当前实现中不支持多个值)
So what you do is... In the font files folder put an htaccess file with the following in it.
also in your remote CSS file, the font-face declaration needs the full absolute URL of the font-file (not needed in local CSS files):
e.g.
That will fix the issue. One thing to note is that you can specify exactly which domains should be allowed to access your font. In the above htaccess I have specified that everyone can access my font with
"*"
however you can limit it to:A single URL:
Header set Access-Control-Allow-Origin http://example.com
Or a comma-delimited list of URLsAccess-Control-Allow-Origin: http://site1.com,http://site2.com
(Multiple values are not supported in current implementations)
根据官方文档,
如果您使用标头 ,浏览器不喜欢它也使用
标题。相反,他们希望您明确允许他们的起源。如果您仍然想允许所有来源,您可以执行一些简单的 Apache 魔法来使其正常工作(确保您已启用
mod_headers
):浏览器需要发送
Origin
所有跨域请求的标头。文档明确指出,如果您正在接受/计划接受请求,则需要在Access-Control-Allow-Origin
标头中回显此标头。这就是这个Header
指令的作用。According to the official docs, browsers do not like it when you use the
header if you're also using the
header. Instead, they want you to allow their origin specifically. If you still want to allow all origins, you can do some simple Apache magic to get it to work (make sure you have
mod_headers
enabled):Browsers are required to send the
Origin
header on all cross-domain requests. The docs specifically state that you need to echo this header back in theAccess-Control-Allow-Origin
header if you are accepting/planning on accepting the request. That's what thisHeader
directive is doing.不幸的是,接受的答案对我不起作用,因为我的网站 CSS 文件 @import 字体 CSS 文件,并且这些文件都存储在 Rackspace 云文件 CDN 上。
由于 Apache 标头永远不会生成(因为我的 CSS 不在 Apache 上),因此我必须执行以下操作:
看看是否可以只使用 #1,因为第二个需要一些命令行工作。
要在 #1 中添加自定义标头:
重复如果您需要继续并执行 #2,那么您将需要一个带有 CURL 的命令行
从返回的结果中提取 X-Auth-Token 和 X-Storage-Url 的值
当然,这仅当您使用 Rackspace CDN 时,该过程才有效。其他 CDN 可能会提供类似的工具来编辑对象标头和更改内容类型,所以也许您会很幸运(并在此处发布一些额外信息)。
The accepted answer doesn't work for me unfortunately, since my site CSS files @import the font CSS files, and these are all stored on a Rackspace Cloud Files CDN.
Since the Apache headers are never generated (since my CSS is not on Apache), I had to do several things:
See if you can get away with just #1, since the second requires a bit of command line work.
To add the custom header in #1:
If you need to continue and do #2, then you'll need a command line with CURL
From the results returned, extract the values for X-Auth-Token and X-Storage-Url
Of course, this process only works if you're using the Rackspace CDN. Other CDNs may offer similar facilities to edit object headers and change content types, so maybe you'll get lucky (and post some extra info here).
对于基于 Java 的应用程序,请将其添加到您的 web.xml 文件中:
For Java based Application add this to your web.xml file:
在请求ajax的file.php中,可以设置值标头。
In your file.php of request ajax, can set value header.