[英]How to change MIME type with script in ReactJS
I am trying to load an external script into my react file, and it is saying: refused to execute script from 'whereMyScriptIs" because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. 我正在尝试将外部脚本加载到我的react文件中,这是说:拒绝从“ whereMyScriptIs”执行脚本,因为其MIME类型(“ text / html”)不可执行,并且启用了严格的MIME类型检查。
How can I change the type to make it text/javascript? 如何更改类型以使其为text / javascript? I have put the code below where I included the script. 我将代码放在包含脚本的位置下面。 Any help would be appreciated! 任何帮助,将不胜感激!
componentDidMount () {
const script = document.createElement("script");
script.src = "./live_w_locator.js";
script.async = true;
script.type = "text/javascript";
document.body.appendChild(script);
} }
You should not include external javascript files like this. 您不应包含此类外部javascript文件。 Instead you could simply add a import './live_w_locator'
to the top of your file. 相反,您可以简单地在文件顶部添加import './live_w_locator'
。
text/javascript
is obsolete text/javascript
已过时 application/javascript
is the current official MIME type for JS application/javascript
是JS的当前官方MIME类型 Instead of script.type = "text/javascript"
you need to make sure script type is application/javascript
您需要确保脚本类型是application/javascript
而不是script.type = "text/javascript"
componentDidMount () {
const script = document.createElement("script");
script.src = "./live_w_locator.js";
script.async = true;
script.type = "application/javascript"; // notice the change here
document.body.appendChild(script);
You can read more about Media Types on Iana.org 您可以在Iana.org上阅读有关媒体类型的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.