首页 > 解决方案 > Flask:加载模块脚本失败:服务器以“text/html”的非 JavaScript MIME 类型响应

问题描述

    <script
      type="module"
      src="{{ url_for('static', filename='js/app.js') }}"
    ></script

我在 html 的正文底部包含了这个脚本标签。由于某些原因,当我使用烧瓶运行在本地运行服务器时,我在浏览器控制台中出现以下错误消息:

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

为什么加载错误的 MIME 类型。我什至不确定是否有更多相关的细节。我会尽我所能来填补空白。

标签: javascriptflask

解决方案


我找到了解决方案。在 Flask 中,如果你像这样导入文件

import SomeClass from './utils'

然后它将标头中的内容类型设置为“html/text”,它会出错。要修复此导入,如下所示:

import SomeClass from './utils.js'

它将正确设置内容类型标头


推荐阅读