加载模块脚本失败:服务器以“text/plain”的非 JavaScript MIME 类型响应。每个 HTML 规范对模块脚本执行严格的 MIME 类型检查。
尝试将此行添加到我的 HTML 代码后,我在 chrome 中收到此错误:
<script type="module">import * as hello from './__target__/hello.js'; window.hello = hello;</script>
<!-- From the official documentation of Transcrypt -->
Run Code Online (Sandbox Code Playgroud)
我一直在尝试修复几个小时,有人建议将类型更改为 text/javascript 并使用 src 标记(src = './__ target__/hello.js'),但我需要在 hello.js 中导入一些内容
修复: 好的,我从命令行使用“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()
Run Code Online (Sandbox Code Playgroud)
Ech*_*cho 10
这个问题(以及所涉及的脚本)对我帮助很大。我已经修改了答案中提供的脚本,使其与 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()
Run Code Online (Sandbox Code Playgroud)
显着的变化是:-
将SimpleHTTPServer更改为http.server。从 python3 开始,SimpleHTTPServer 已经成为 http.server 的一部分
将SocketServer模块更改为socketserver
这些功能是向后兼容的。
您的网络服务器正在使用 提供服务hello.js,Content-Type: text/plain但浏览器不允许这样做,因为它不是 JavaScript。
您需要更新您的 Web 服务器配置,以便它*.js以Content-Type: application/javascript.
| 归档时间: |
|
| 查看次数: |
16373 次 |
| 最近记录: |