简体   繁体   中英

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". Strict MIME type checking is enforced for module scripts per HTML spec.

I get this error in chrome after trying to add this line to my HTML code:

<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

FIXED: Ok I was starting the server with 'python -m http.server' from command line, I just replaced it with this python2 script:

#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.

#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 . From python3 onwards , SimpleHTTPServer has become a part of http.server

  2. Changed SocketServer module to 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.

You need to update your web server configuration so that it serves that file (and presumably all *.js files) with Content-Type: application/javascript .

Another option is renaming the Javascript file to *.mjs instead of *.js . 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. Changing it to application/javascript it worked again. 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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM