简体   繁体   English

如何允许从其他域引用CSS文件?

[英]How do I allow CSS files to be referenced from other domains?

I set up two websites. 我建立了两个网站。 One, http://static.example.com/ , is hosting static files. 其中一个是http://static.example.com/ ,它正在托管静态文件。 Another one, http://www.example.com/ hosts a web application. 另一个是http://www.example.com/托管一个Web应用程序。

When, within http://www.example.com/ , I reference images from the static site such as http://static.example.com/logo.png , the image is displayed correctly. http://www.example.com/中 ,当我从静态网站(例如http://static.example.com/logo.png)引用图像时,图像会正确显示。 However, when I put: 但是,当我把:

<link rel="stylesheet" type="text/css" href="http://static.example.com/demo.css" />

on the page, the style from demo.css is not applied to the page. 在页面上, demo.css的样式不适用于页面。

In Chrome's Developer Tools: 在Chrome的开发者工具中:

  • In Network tab, I can clearly see the demo.css file being requested and received (HTTP 200 or HTTP 304) by the browser. 在“ 网络”选项卡中,我可以清楚地看到浏览器正在请求和接收的demo.css文件(HTTP 200或HTTP 304)。 If open the file in the new tab, the CSS code is displayed correctly. 如果在新选项卡中打开文件,则会正确显示CSS代码。

  • In Network tab, when I click on the file and open Response sub-tab, it shows: “This request has no response data available.” 在“ 网络”选项卡中,当我单击该文件并打开“ 响应”子选项卡时,它会显示: “此请求没有可用的响应数据。”

  • In Sources tab, there is static.example.com node and it contains demo.css , but when I click on demo.css , the panel at the right remains empty, as if the file was empty. 选项卡中,有static.example.com节点,它包含demo.css ,但是当我点击demo.css ,在右侧面板依然空荡荡的,如果该文件是空的。

Is there a sort of cross-origin policy for CSS files? 是否有一种CSS文件的跨源策略?

If I replace the style file by, for instance, http://cdn.sstatic.net/Sites/stackoverflow/all.css , the one used by Stack Overflow, the style is applied to the page. 如果我用例如http://cdn.sstatic.net/Sites/stackoverflow/all.css(Stack Overflow使用的那个)替换样式文件,则样式将应用于页面。

Notes: 笔记:

  • The CSS file demo.css contains just body{color:red;} . CSS文件demo.css只包含body{color:red;} The HTML page contains a body with a <p>Hello, World!</p> inside. HTML页面包含一个内部带有<p>Hello, World!</p>的正文。 The browser shows black text. 浏览器显示黑色文本。 I would expect it to show red text instead. 我希望它能显示红色文字。

  • I replaced the actual TLD by example.com , the actual structure and domain names are very similar. 我用example.com替换了实际的TLD,实际的结构和域名非常相似。

  • I can reproduce the issue with Chromium 49 and Firefox 45. 我可以用Chromium 49和Firefox 45重现这个问题。

Appears to be related to the content-type . 似乎与content-type

For the CSS file that doesn't work: 对于不起作用的CSS文件:

Content-Type:text/plain

For the CSS file that does work: 对于有效的CSS文件:

Content-Type:text/css

As noted in the MDN docs MDN文档中所述

Here are some examples of resources which may be embedded cross-origin: 以下是可能嵌入跨源的资源的一些示例:

  • CSS with <link rel="stylesheet" href="..."> . 带有<link rel="stylesheet" href="..."> CSS。 Due to the relaxed syntax rules of CSS, cross-origin CSS requires a correct Content-Type header. 由于CSS的宽松语法规则,跨源CSS需要正确的Content-Type标头。 Restrictions vary by browser: IE , Firefox , Chrome , Safari (scroll down to CVE-2010-0051) and Opera . 限制因浏览器而异: IEFirefoxChromeSafari (向下滚动到CVE-2010-0051)和Opera

Look into the Content-type headers and ensure they're correct. 查看Content-type标头并确保它们是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM