简体   繁体   English

如何在ReactJS中使用脚本更改MIME类型

[英]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.

相关问题 注册 ServiceWorker 失败:脚本的 MIME 类型不受支持 - reactjs - Failed to register a ServiceWorker: The script has an unsupported MIME type - reactjs 设置/更改iFrame的MIME类型? - Set / Change MIME type of iFrame? 有什么有用的哑剧吗<script type=“mime” src=“…”> except for JS? - Is there any useful mime for <script type=“mime” src=“…”> except for JS? JSONP脚本返回MIME类型错误 - JSONP script returning MIME Type error 如何使用Google Apps脚本列出云端硬盘中不是“ google” MIME类型的所有文件? - How to list all files in Drive that are not “google” mime type using Google Apps Script? 我该如何解决“将资源解释为脚本,但以MIME类型text / html进行传输” - How to do i resolve “Resource interpreted as Script but transferred with MIME type text/html” 如何从MIME类型获取媒体类型? - How to get a media type from a MIME type? ReactJS,如何从onChange下拉菜单更改表单类型 - ReactJS , How to change form type, from onChange dropdown ReactJs,如何将输入类型从“文本”动态更改为“选择”? - In ReactJs, How to dynamically change the Input type from "text" to "select"? 加载模块脚本失败:服务器以非 JavaScript MIME 类型“”响应。 强制执行严格的 MIME 类型检查 - Failed to load module script: The server responded with a non-JavaScript MIME type of “”. Strict MIME type checking is enforced
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM