JavaScript MIME type warning on Firefox

我与影子孤独终老i 提交于 2020-08-09 17:20:57

问题


I am loading the java-script file in Django template:

<script type="application/javascript" src="{% static 'online-v3.js' %}"></script>

It is loading properly on Chrome. But, on Firefox I get the following warning:

The script from “http://127.0.0.1:8000/static/myscript.js” was loaded even though its MIME type (“text/plain”) is not a valid JavaScript MIME type.

I fear that due to this problem, on some browser the JS file might not load at all.

What is the possible reason for this and how can I solve this issue?


回答1:


Remove the type or change it to "text/javascript".

In html5 spec the type is not required unless it is not javascript




回答2:


Your server is wrongly configured and is serving .js files with a wrong Content-Type header of text/plain.

In the future Firefox might start blocking scripts with incorrect MIME types.




回答3:


To add onto evilpie's answer, the root cause for me was using the basic py -m http.server for local testing. In order to correctly assign .js files the 'text/javascript' MIME type, I instead use this script from Github user HaiyangXu.

# -*- coding: utf-8 -*-
#test on python 3.4 ,python of lower version  has different module organization.
import http.server
from http.server import HTTPServer, BaseHTTPRequestHandler
import socketserver

PORT = 8080

Handler = http.server.SimpleHTTPRequestHandler

Handler.extensions_map={
        '.manifest': 'text/cache-manifest',
    '.html': 'text/html',
        '.png': 'image/png',
    '.jpg': 'image/jpg',
    '.svg': 'image/svg+xml',
    '.css': 'text/css',
    '.js':  'application/x-javascript',
    '': 'application/octet-stream', # Default
    }

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

print("serving at port", PORT)
httpd.serve_forever()

Source: https://gist.github.com/HaiyangXu/ec88cbdce3cdbac7b8d5



来源:https://stackoverflow.com/questions/57432292/javascript-mime-type-warning-on-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!