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

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

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

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


当前回答

我想从理解问题开始

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

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

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

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

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

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

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

其他回答

我也有这个错误的问题,并想出了一个解决方案。这并没有解释错误发生的原因,但似乎在某些情况下修复了它。

在css文件的路径前加上一个正斜杠/,如下所示:

<link rel=“stylesheet” href=“/css/bootstrap.min.css”>

我也遇到过类似的问题。在这个奇妙的Stack Overflow页面中探索解决方案。

user54861的响应(大小写不匹配的名字)让我很好奇,再次检查我的代码,并意识到“我没有上传两个js文件,我在头标签中加载了它们”。: -)

当我上传他们的问题跑了!并且代码运行和页面呈现没有任何其他错误!

所以,这个故事的寓意是,不要忘记确保你所有的js文件都上传到了页面需要它们的地方。

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已经存在。

@Rob Sedgwick的回答给了我一个指针,然而,在我的情况下,我的应用程序是一个春季启动应用程序。所以我只是在我的安全配置中为相关文件的路径添加了排除…

说明—此解决方案基于springboot…你需要做的可能会根据你使用的编程语言和/或你使用的框架而有所不同

然而,要注意的一点是;

从本质上讲,当每个请求,包括 那些静态内容正在被验证。

所以让我们说一些路径到我的静态内容,导致错误如下;

一个叫做“插件”的路径

http://localhost:8080/plugins/styles/css/file-1.css http://localhost:8080/plugins/styles/css/file-2.css http://localhost:8080/plugins/js/script-file.js

还有一个路径叫做pages

http://localhost:8080/pages/styles/css/style-1.css http://localhost:8080/pages/styles/css/style-2.css http://localhost:8080/pages/js/scripts.js

然后,我只需在Spring Boot安全配置中添加如下排除项;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers(<comma separated list of other permitted paths>, "/plugins/**", "/pages/**").permitAll()
            // other antMatchers can follow here
    }

}

从身份验证中排除这些路径“/plugins/**”和“/pages/**”可以消除错误。


干杯!

我有同样的问题,几分钟后,我破译,我错过了添加文件扩展名到我的头。所以我改了下面这行:

<link uic-remove rel="stylesheet" href="css/bahblahblah">

to

<link uic-remove rel="stylesheet" href="css/bahblahblah.css">