我有这个问题。Chrome继续返回此错误

资源解释为样式表,但以MIME类型text/html传输

受此错误影响的文件只有Style、chosen和jquery-gentleselect(以相同方式导入索引的其他CSS文件工作良好且没有错误)。我已经检查了我的MIME类型和文本/css已经在css上。

老实说,我想从理解问题开始(这似乎是我一个人做不到的事情)。


当前回答

对于任何可能有这种问题的人。 当我遇到这个问题时,我正在用PHP构建一个自定义MVC。

我能够通过将我的资产(css/js/images)文件设置为绝对路径来解决这个问题。 而不是使用url像href="css/style.css"使用整个当前url来加载它。例如,如果您在http://example.com/user/5,它将尝试在http://example.com/user/5/css/style.css加载。

为了解决这个问题,你可以在资产url的开头添加一个/(即href="/css/style.css")。这将告诉浏览器从url的根目录加载它。在本例中,它将尝试加载http://example.com/css/style.css。

希望这篇评论对你有所帮助。

其他回答

使用ReactJs,当我添加样式文件到index.html使用相对链接,如

<link rel="stylesheet" href="./css/style.css"> .

然后我导航到一条路线,说;localhost:3000/artist和刷新,我得到错误。

错误消失后,我取代相对链接与绝对链接说;

<link rel=“stylesheet” href=“http://localhost/project/public/css/style.css”。

In my case, same error was coming with JSP. This is how I got to the root of this issue: I went to the Network tab and clearly saw that the browser request to fetch the css was returning response header having "Content-type" : "text/html". I opened another tab and manually hit the request to fetch the css. It ended up returning a html log in form. Turns out that my web application had a url mapping for path = / Tomcat servlet entry : (@WebServlet({ "/, /index", }). So, any unmatching request URLs were somehow being matched to / and the corresponding servlet was returning a Log in html form. I fixed it by removing the mapping to /

此外,tomcat配置wrt处理css MIME-TYPE已经存在。

I faced the same issue while configuring Postfix Admin 3.2. According to the official documentation, the whole Postfix Admin content should be extracted into a separate directory, i.e. /srv/postfixadmin and not the document root. Only the /srv/postfixadmin/public directory should be symlinked into /var/www/html document root. I have just extracted the whole content into /var/www/html. Having played with Nginx server block settings, I managed example.com/postfixadmin resolving from /var/www/html/postfixadmin/public. Nevertheless, images and CSS were not available with 404 status code. The stylesheets were broken. I got the error message OP quoted with respective 404 entries in access log.

在我的情况下,我只是移动/var/www/html/postfixadmin到/srv/postfixadmin和ln -s /srv/postfixadmin/公共/var/www/html/postfixadmin。这完全解决了问题。 这是参考资料。

我想从理解问题开始

浏览器向服务器发出HTTP请求。然后服务器做出一个HTTP响应。

请求和响应都由一堆头部和一个(有时是可选的)包含一些内容的主体组成。

如果有一个主体,那么其中一个头是Content-Type,它描述了主体是什么(它是HTML文档吗?一个图像?表单提交的内容?等等)。

当您请求样式表时,服务器会告诉浏览器这是一个HTML文档(Content-Type: text/ HTML),而不是一个样式表(Content-Type: text/css)。

我已经查过了。Type and text/css已经在css上了。

那么,您的服务器的其他部分使样式表具有错误的内容类型。

使用浏览器开发人员工具的Net选项卡检查请求和响应。

有同样的错误,因为我忘记发送一个正确的头

header("Content-type: text/css; charset: UTF-8");
print 'body { text-align: justify; font-size: 2em; }';