php 资源被解释为文档但使用 MIME 类型 text/css 传输

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3899426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:24:34  来源:igfitidea点击:

Resource interpreted as document but transferred with MIME type text/css

phpcsshttp-headers

提问by Emmanuel

Resource interpreted as document but transferred with MIME type text/css

资源被解释为文档但使用 MIME 类型 text/css 传输

This is the error that's showing up in Google Chrome Inspector.

这是 Google Chrome Inspector 中显示的错误。

The file is: http://www.doanddare.org/css/style.css

该文件是:http: //www.doanddare.org/css/style.css

It is being rendered by a php processor.

它由 php 处理器呈现。

How can I fix this error?

我该如何解决这个错误?

The headers being returned are:

返回的标题是:

Request Headers

请求头

Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Cache-Control:max-age=0
User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3

Response Headers

响应头

HTTP/1.1 200 OK
Date: Sun, 10 Oct 2010 07:35:04 GMT
Server: Apache
content-style-type: text/css
Expires: Thu, 10 Oct 2030 03:50:23 GMT
Cache-Control: max-age=631138519, public
Content-Encoding: gzip
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/css
Connection: Keep-Alive

Could it be something to do with the request headers being sent as a document and the response headers returned as a css file?

这可能与作为文档发送的请求标头和作为 css 文件返回的响应标头有关吗?

If so, how should I control the request headers?

如果是这样,我应该如何控制请求标头?

Thanks for any help.

谢谢你的帮助。

回答by mauris

I only get error that when I view the specific resource on Chrome. This also happens when I try to open a Javascript file (.js).

我只会在 Chrome 上查看特定资源时收到错误消息。当我尝试打开 Javascript 文件 (.js) 时也会发生这种情况。

My conclusion is that when you open a component file (.css, .js) in Chrome, it will try to interpret as document (showing output on the screen), instead of interpreting it as its native MIME type.

我的结论是,当您在 Chrome 中打开组件文件(.css、.js)时,它会尝试将其解释为文档(在屏幕上显示输出),而不是将其解释为其原生 MIME 类型。

Say a Javascript file. If you enter it in Chrome, it should be interpreted as document (showing output on the screen), instead of Chrome executing the script (that's what its native MIME type does).

说一个 Javascript 文件。如果你在 Chrome 中输入它,它应该被解释为文档(在屏幕上显示输出),而不是 Chrome 执行脚本(这就是它的原生 MIME 类型所做的)。

That message is Google Chrome only and is negligible, as it only appear when you open a component file.

该消息仅适用于 Google Chrome,可以忽略不计,因为它仅在您打开组件文件时出现。

回答by Panu Logic

I get the same error and I think I know why. If you look at your request headers you see they include:

我遇到了同样的错误,我想我知道为什么。如果您查看请求标头,您会看到它们包括:

Accept:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,/;q=0.5

接受:application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png, /;q=0.5

They especially do NOT include "text/css". So Chrome is telling the server it will NOT accept a response of type "text/css" and when it gets such anyway it reports that happening on its console.

它们尤其不包括“text/css”。所以 Chrome 告诉服务器它不会接受类型为“text/css”的响应,并且当它无论如何得到这样的响应时,它会在其控制台上报告发生的情况。

When I ordinarily include a CSS-stylesheet into an html-page the error-message does not show up because in those cases Chrome request-header says:

当我通常将 CSS 样式表包含到 html 页面时,不会显示错误消息,因为在这些情况下,Chrome 请求标头表示:

Accept: text/css

But if I try to set

但是如果我尝试设置

iframe.src = someCssFile.css

I get the message. So it gives me the clue that I'm trying to load CSS to somewhere where it is not expected. It does load it, but console gives me the message.

我收到了消息。所以它给了我一个线索,我正在尝试将 CSS 加载到不期望的地方。它确实加载了它,但控制台给了我消息。

And really it is not an error, it is a Warning, that's what Chrome calls it. A Warning. But I agree it's good to know what's causing it.

实际上,这不是错误,而是警告,Chrome 就是这样称呼它的。一个警告。但我同意知道是什么导致它是件好事。

I don't think there's any way to explicitly control the request-headers Chrome sends to the server. Browsers just interpret HTML and based on that come up with the headers they send.

我认为没有任何方法可以明确控制 Chrome 发送到服务器的请求标头。浏览器只是解释 HTML 并基于它提出他们发送的标题。