【问题标题】:Spring Boot returns js files as MIME 'application/json' instead of 'text/javascript'Spring Boot 将 js 文件返回为 MIME 'application/json' 而不是 'text/javascript'
【发布时间】:2021-11-08 22:43:41
【问题描述】:

我在 Spring Boot 中使用 Thymeleaf。在加载项目站点的页面时,它说我的脚本被阻止了,因为它是以 json 而不是 text/javascript 的形式返回的。

我使用标准 Thymeleaf 模板将脚本包含在 html 中,如下所示:

  <script type="text/javascript" th:src="@{/js/admin-register-users.js}" defer></script>

我已通过以下方式设置资源处理:


@Configuration
public class WebConfig implements WebMvcConfigurer {
 
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        registry.addResourceHandler("/img/**").addResourceLocations("classpath:/static/img/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");

        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}

由于某种原因在浏览器中出现错误:

The resource from “https://localhost:8090/js/admin-register-users.js” was blocked due to MIME type (“application/json”) mismatch (X-Content-Type-Options: nosniff).

【问题讨论】:

    标签: javascript spring-boot mime


    【解决方案1】:

    原来是找不到文件,所以服务器返回了一个json响应,但是那个响应被阻塞了,无法读取。

    长话短说,与资源配置无关。文件不是从 TypeScript 生成的。所以服务器只找到了一个空的js文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      • 2012-06-02
      • 2012-07-10
      • 2014-01-17
      • 1970-01-01
      相关资源
      最近更新 更多