一零五一、关于Django项目Google浏览器报错because its MIME type (‘text/plain‘) isnot executable, and strict MIMEtype

背景:

登录界面明显有文件未加载到,翻网络,没有404,翻控制台,看到一堆爆红 

控制台报出如下错误:

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:8001/static/admin/simpleui-x/js/vue.min.js?_=2021.4.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:8001/static/admin/simpleui-x/elementui/index.js?_=2021.4.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:8001/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:8001/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:8001/static/admin/simpleui-x/particles/app.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.

找到原因,总结后如下:

第一种错误是:

Refused to display ‘http://localhost:8000/cdny/student_manage/’ in a frame because it set ‘X-Frame-Options’ to ‘deny’.

解决方法:
在settings.py中加一条配置:

X_FRAME_OPTIONS = 'SAMEORIGIN'  
# DENY :表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许  
# SAMEORIGIN :表示该页面可以在相同域名页面的 frame 中展示  
# ALLOW-FROM uri :表示该页面可以在指定来源的 frame 中展示  

第二种错误是:

Refused to execute script from ‘http://127.0.0.1:8000/static/layui/layui.js’ because its MIME type (‘text/plain’) is not executable, and strict MIME type checking is enabled.

解决方法:
在settings.py中加一条配置:

SECURE_CONTENT_TYPE_NOSNIFF = False

这个X-Content-Type-Options是nosniff,错误解释:当你请求一个文件从raw.githubusercontent.com,gist.githubusercontent.com,bitbucket.org或者gitlab.com,他们通常提供(在JavaScript,HTML,CSS,以及一些其他文件类型的情况下)用Content-Type的text/plain。结果,大多数现代浏览器实际上不会将其解释为JavaScript,HTML或CSS。

还有一种情况可能是Windows系统上不知道什么时候破坏了注册表,把扩展名为js的文件类型注册为 ‘text/plain’, 应该为’application/javascript’。

要解决这个问题,请在 Django项目 setting.py 中 末尾添加如下代码,强制Django使用’application/javascript’ 作为 js文件类型
第三种解决方法:

#在settings.py末尾加入
import mimetypes
mimetypes.add_type('text/css', '.css')
mimetypes.add_type('application/javascript', '.js')

重点:

清除浏览器缓存,不然不会即刻生效!!!

效果:

 


阿弥陀佛

猜你喜欢

转载自blog.csdn.net/m0_54925305/article/details/129736115