简体   繁体   English

无法加载模块脚本:服务器以“text/plain”的非 JavaScript MIME 类型响应

[英]Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain"

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain".无法加载模块脚本:服务器以“text/plain”的非 JavaScript MIME 类型响应。 Strict MIME type checking is enforced for module scripts per HTML spec.根据 HTML 规范,对模块脚本强制执行严格的 MIME 类型检查。

I get this error in chrome after trying to add this line to my HTML code:尝试将此行添加到我的 HTML 代码后,我在 chrome 中收到此错误:

<script type="module">import * as hello from './__target__/hello.js'; window.hello = hello;</script>
<!-- From the official documentation of Transcrypt -->

I've been trying to fix for hours, someone suggested to change the type to text/javascript and to use the src tag (src = './__ target__/hello.js') but I need some imports in hello.js我已经尝试修复了几个小时,有人建议将类型更改为 text/javascript 并使用 src 标记(src = './__ target__/hello.js'),但我需要在 hello.js 中导入一些内容

FIXED: Ok I was starting the server with 'python -m http.server' from command line, I just replaced it with this python2 script:修正:好的,我从命令行用'python -m http.server'启动服务器,我只是用这个python2脚本替换它:

#Use to create local host
import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
Handler.extensions_map.update({
    ".js": "application/javascript",
});

httpd = SocketServer.TCPServer(("", PORT), Handler)

print ("Serving at port", PORT)
print(Handler.extensions_map[".js"])
httpd.serve_forever()

This question (and the script involved) really helped me a lot.这个问题(以及所涉及的脚本)确实对我帮助很大。 I have modified the script provided in the answer to make it compatible with python3.我已经修改了答案中提供的脚本,使其与 python3 兼容。

#Use to create local host
import http.server
import socketserver

PORT = 1337

Handler = http.server.SimpleHTTPRequestHandler
Handler.extensions_map.update({
      ".js": "application/javascript",
});

httpd = socketserver.TCPServer(("", PORT), Handler)
httpd.serve_forever()

The notable changes are:-显着的变化是:-

  1. Changed SimpleHTTPServer to http.server .SimpleHTTPServer更改为http.server From python3 onwards , SimpleHTTPServer has become a part of http.server从 python3 开始,SimpleHTTPServer 已经成为 http.server 的一部分

  2. Changed SocketServer module to socketserverSocketServer模块更改为socketserver

The functions are backward compatible.这些功能向后兼容。

Your web server is serving hello.js with Content-Type: text/plain , which is disallowed by the browser because it's not JavaScript.您的 Web 服务器正在使用Content-Type: text/plain提供hello.js ,浏览器不允许这样做,因为它不是 JavaScript。

You need to update your web server configuration so that it serves that file (and presumably all *.js files) with Content-Type: application/javascript .您需要更新您的 Web 服务器配置,以便它使用Content-Type: application/javascript为该文件(可能还有所有*.js文件)提供服务。

Another option is renaming the Javascript file to *.mjs instead of *.js .另一种选择是将 Javascript 文件重命名为*.mjs而不是*.js https://github.com/python/cpython/issues/75896 https://github.com/python/cpython/issues/75896

In my case (Windows 10 + Django 2.2.25) it was windows registry Computer\HKEY_CLASSES_ROOT.js has a Content Type key set to text/plain.在我的情况下(Windows 10 + Django 2.2.25)它是 windows 注册表 Computer\HKEY_CLASSES_ROOT.js 的 Content Type 键设置为 text/plain。 Changing it to application/javascript it worked again.将其更改为 application/javascript 它再次起作用。 Django use view.py and static.py to serve static file (static.serve) that uses mimetype (from Python library). Django use view.py and static.py to serve static file (static.serve) that uses mimetype (from Python library). mimetype.py call mimetypes.guess_type() mimetype.py 调用 mimetypes.guess_type()

I am hosting my Angular compiled website with IIS and had to change the mime type in it for .js files from application/javascript to text/javascript , then it worked for some reason.我正在使用 IIS 托管我的 Angular 编译网站,并且不得不将其中的.js文件的 mime 类型从application/javascript更改为text/javascript ,然后它出于某种原因工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加载模块脚本失败:服务器以“text/plain”的非 JavaScript MIME 类型响应。 对模块强制执行严格的 MIME 类型检查 - Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking is enforced for module 加载模块脚本失败:服务器以“text/plain”的非 JavaScript MIME 类型响应。 严格的 MIME 类型检查 i - Failed to load module script: The server responded with a non-JavaScript MIME type of "text/plain". Strict MIME type checking i 加载模块脚本失败:服务器以非 JavaScript、CSS MIME 类型“text/x-scss”响应 - Failed to load module script: The server responded with a non-JavaScript, CSS MIME type of “text/x-scss” Vue.js 3 - “加载模块脚本失败:服务器以非 JavaScript MIME 类型“text/html”响应 - Vue.js 3 - “Failed to load module script: The server responded with a non-JavaScript MIME type of ”text/html" Flask:加载模块脚本失败:服务器以“text/html”的非 JavaScript MIME 类型响应 - Flask: Failed to load module script: The server responded with a non-JavaScript MIME type of “text/html” 加载模块脚本失败:服务器以非 JavaScript MIME 类型“”响应。 强制执行严格的 MIME 类型检查 - Failed to load module script: The server responded with a non-JavaScript MIME type of “”. Strict MIME type checking is enforced 加载模块脚本失败:服务器响应非 JavaScript MIME 类型“” - Failed to load module script: The server responded with a non-JavaScript MIME type of "" 从&#39;./image.jpeg&#39;结果导入图像无法加载模块脚本服务器以非JavaScript MIME类型“ image / jpegerror”响应 - Import image from './image.jpeg' results Failed to load module script The server responded with a non-JavaScript MIME type of "image/jpegerror 服务器以“text/html”的非 JavaScript MIME 类型响应 - 节点 - The server responded with a non-JavaScript MIME type of "text/html" - Node 无法加载模块脚本:需要 JavaScript 模块脚本,但服务器响应 MIME 类型为“text/html” - Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM