Ger*_*kov 9 html javascript python http-headers flask
我有这个 html 文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link
rel="stylesheet"
href="../../../static/css/style.css"
type="text/css" />
<link
rel="stylesheet"
href="../../../static/css/user_header.css"
type="text/css" />
<!--suppress HtmlUnknownTarget -->
<link rel="shortcut icon" href="/favicon.png">
<script type="module" src="../../../static/js/node_connect.js" ></script> <-- Error
<script src="../../../static/lib/jquery.min.js"></script>
<script src="../../../static/lib/selfserve.js"></script>
</head>
Run Code Online (Sandbox Code Playgroud)
有问题的部分是node_connect.js文件。本地启动Flask web工具(Python 3.7.2)时,打开页面时控制台报如下错误:
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.
Run Code Online (Sandbox Code Playgroud)
检查标题:
Content-Type: text/plain; charset=utf-8
Run Code Online (Sandbox Code Playgroud)
然而,在生产中(当通过 Gunicorn 启动时)它给出了:
Content-Type: application/javascript; charset=utf-8
Run Code Online (Sandbox Code Playgroud)
我猜测 Web 服务器(Apache)在生产情况下为它们提供服务,但另一件事是在测试此 Web 工具的其他页面时,它们都可以正常工作并正确加载 javascript 文件(即使它们的内容类型是文本/清楚的)。然而,我注意到,区别在于类型。
这适用于我的情况:
<script src="../../static/js/translator/library/materialize.js"></script>
Run Code Online (Sandbox Code Playgroud)
或这个:
<script type="application/javascript" src="../../static/js/translator/library/materialize.js"></script>
Run Code Online (Sandbox Code Playgroud)
当然,我尝试对有问题的javascript文件进行此操作并收到以下错误(这意味着它现在已加载):
Uncaught SyntaxError: Cannot use import statement outside a module
Run Code Online (Sandbox Code Playgroud)
据我研究,这基本上意味着我需要将类型设置为module(但这会使浏览器拒绝 .js 文件)。
有人可以帮我解决这个问题吗?
Har*_*wal 12
这是另一个技巧,无需更改注册表中的任何内容:
将其放在导入 Flask 之前:
# fix windows registry stuff
import mimetypes
mimetypes.add_type('application/javascript', '.js')
mimetypes.add_type('text/css', '.css')
Run Code Online (Sandbox Code Playgroud)
Fil*_*nik 10
问题是由 Flask 如何猜测每个静态文件的内容类型引起的。为此,flask 导入mimetypes并调用mimetype, encoding = mimetypes.guess_type(download_name)
该模块从多个来源创建已知 mime 类型的数据库,并使用它返回 mime 类型。
在 Linux 和 MacOS 下mimetypes.py查看文件内部:
knownfiles = [
"/etc/mime.types",
"/etc/httpd/mime.types", # Mac OS X
"/etc/httpd/conf/mime.types", # Apache
"/etc/apache/mime.types", # Apache 1
"/etc/apache2/mime.types", # Apache 2
"/usr/local/etc/httpd/conf/mime.types",
"/usr/local/lib/netscape/mime.types",
"/usr/local/etc/httpd/conf/mime.types", # Apache 1.2
"/usr/local/etc/mime.types", # Apache 1.3
]
Run Code Online (Sandbox Code Playgroud)
但在 Windows 下它会查看注册表内部:
with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr:
for subkeyname in enum_types(hkcr):
try:
with _winreg.OpenKey(hkcr, subkeyname) as subkey:
# Only check file extensions
if not subkeyname.startswith("."):
continue
# raises OSError if no 'Content Type' value
mimetype, datatype = _winreg.QueryValueEx(
subkey, 'Content Type')
if datatype != _winreg.REG_SZ:
continue
self.add_type(mimetype, subkeyname, strict)
Run Code Online (Sandbox Code Playgroud)
因此,要解决 Flask 认为 .js 文件实际上就是text/plain所需的问题,只需打开 regedit 并将此注册表项调整为application/javascript:
| 归档时间: |
|
| 查看次数: |
9303 次 |
| 最近记录: |