简体   繁体   中英

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.

How can I change the type to make it 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. Instead you could simply add a import './live_w_locator' to the top of your file.

  • text/javascript is obsolete
  • application/javascript is the current official MIME type for JS

Instead of script.type = "text/javascript" you need to make sure script type is application/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM