分类: Python
Django3.x报错"Refused to execute script from '<URL>' because its MIME type ('text/plain') "问题处理

Win11系统在本地启动项目后页面元素不能正常显示,js未能正常加载,报错如下:

Refused to execute script from '<URL>' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'http://127.0.0.1:8000/static/admin/simpleui-x/js/vue.min.js?_=2022.7.29' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'http://127.0.0.1:8000/static/admin/simpleui-x/elementui/index.js?_=2022.7.29' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'http://127.0.0.1:8000/static/admin/simpleui-x/js/login.js?_=3.3' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'http://127.0.0.1:8000/static/admin/simpleui-x/particles/particles.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.
127.0.0.1/:1 Refused to execute script from 'http://127.0.0.1:8000/static/admin/simpleui-x/particles/app.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.

django-error.png
操作系统把扩展名为js的文件类型注册为 'text/plain', 正确的应为'application/javascript'
问题处理:使用mimetypes模块重新注册解决:

#在settings.py中增加如下代码
import mimetypes
mimetypes.add_type('application/javascript', '.js')

如果有其他文件类型也可以使用mimetypes.add_type添加进去,强刷浏览器或清空缓存重新访问系统恢复正常。


相关博文:

发表新评论