[英]Refused to execute script, strict MIME type checking is enabled?
Why am I getting this error in console?为什么我在控制台中收到此错误?
Refused to execute script from ' https://www.googleapis.com/customsearch/v1?key=API_KEY&q=flower&searchType=image&fileType=jpg&imgSize=small&alt=json ' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.
拒绝从“ https://www.googleapis.com/customsearch/v1?key=API_KEY&q=flower&searchType=image&fileType=jpg&imgSize=small&alt=json ”执行脚本,因为它的 MIME 类型(“application/json”)不可执行,并且启用严格的 MIME 类型检查。
You have a <script>
element that is trying to load some external JavaScript.您有一个
<script>
元素试图加载一些外部 JavaScript。
The URL you have given it points to a JSON file and not a JavaScript program.您给它的 URL 指向一个 JSON 文件,而不是一个 JavaScript 程序。
The server is correctly reporting that it is JSON so the browser is aborting with that error message instead of trying to execute the JSON as JavaScript (which would throw an error).服务器正确报告它是 JSON,因此浏览器会因该错误消息而中止,而不是尝试将 JSON 作为 JavaScript 执行(这会引发错误)。
Odds are that the underlying reason for this is that you are trying to make an Ajax request, have hit a cross origin error and have tried to fix it by telling jQuery that you are using JSONP.很可能这是因为您正在尝试发出 Ajax 请求,遇到了跨源错误,并试图通过告诉 jQuery 您正在使用 JSONP 来修复它。 This only works if the URL provides JSONP (which is a different subset of JavaScript), which this one doesn't.
这仅在 URL 提供 JSONP(它是 JavaScript 的不同子集)时才有效,而这个没有。
The same URL with the additional query string parameter callback=the_name_of_your_callback_function
does return JavaScript though.带有附加查询字符串参数
callback=the_name_of_your_callback_function
的相同 URL 确实返回 JavaScript。
在我的情况下,它是一个未找到的文件,我错误地输入了 javascript 文件的路径。
This result is the first that pops-up in google, and is more broad than what's happening here.这个结果是第一个在 google 中弹出的结果,比这里发生的情况更广泛。 The following will apply to an express server :
以下将适用于快速服务器:
I was trying to access resources from a nested folder.我试图从嵌套文件夹访问资源。
Inside index.html
i had在
index.html
我有
<script src="./script.js"></script>
The static route was mounted at :静态路由安装在:
app.use(express.static(__dirname));
But the script.js
is located in the nested folder as in: js/myStaticApp/script.js
但是
script.js
位于嵌套文件夹中,如下所示: js/myStaticApp/script.js
I just changed the static route to:
我只是将静态路由更改为:
app.use(express.static(path.join(__dirname, "js")));
Now it works :)现在它起作用了:)
Try to use express.static() if you are using Node.js.如果您使用的是 Node.js,请尝试使用express.static() 。
You simply need to pass the name of the directory where you keep your static assets, to the express.static middleware to start serving the files directly.您只需将保存静态资产的目录的名称传递给 express.static 中间件即可开始直接提供文件。 For example, if you keep your images, CSS, and JavaScript files in a directory named public, you can do as below −
例如,如果您将图像、CSS 和 JavaScript 文件保存在名为 public 的目录中,您可以执行以下操作:
ie : app.use(express.static('public'));即: app.use(express.static('public'));
This approach resolved my issue.这种方法解决了我的问题。
Thanks,谢谢,
After searching for a while I realized that this error in my Windows 10 64 bits was related to JavaScript.搜索了一段时间后,我意识到我的 Windows 10 64 位中的这个错误与 JavaScript 相关。 In order to see this go to your browser DevTools and confirm that first.
为了看到这一点,请转到您的浏览器 DevTools 并首先确认。 In my case it shows an error like "MIME type ('application/javascript') is not executable".
就我而言,它显示了一个错误,如“MIME 类型(‘application/javascript’)不可执行”。
If that is the case I've found a solution.如果是这种情况,我已经找到了解决方案。 Here's the deal:
这是交易:
I had the exact same issue a while ago.
不久前我遇到了完全相同的问题。 I think this issue is specific to Windows.
我认为这个问题是特定于 Windows 的。 It is due to the wrong MIME type being set in Windows registry for javascript files.
这是由于在 Windows 注册表中为 javascript 文件设置了错误的 MIME 类型。 I solved the issue by editing the Windows registry with correct content type:
我通过使用正确的内容类型编辑 Windows 注册表解决了这个问题:
regedit -> HKEY_LOCAL_MACHINE\\Software\\Classes -> You will see lot of folders for each file extension -> Just scroll down to ".js" registry and select it -> On the right, if the "Content Type" value is other than application/javascript, then this is causing the problem.
regedit -> HKEY_LOCAL_MACHINE\\Software\\Classes -> 您将看到每个文件扩展名的许多文件夹 -> 只需向下滚动到“.js”注册表并选择它 -> 在右侧,如果“内容类型”值不是应用程序/javascript,那么这就是导致问题的原因。 Right click on Content Type and change the value to application/javascript
右键单击内容类型并将值更改为 application/javascript
Try again in the browser."
在浏览器中重试。”
After that I've realized that the error changes.在那之后,我意识到错误发生了变化。 It doesn't even open automatically in the browser anymore.
它甚至不再在浏览器中自动打开。 PGAdmin, however, will be open on the side bar (close to the calendar/clock).
但是,PGAdmin 将在侧栏上打开(靠近日历/时钟)。 By trying to open in the browser directly ("New PGAdmin 4 window...") it doesn't work either.
通过尝试直接在浏览器中打开(“新 PGAdmin 4 窗口...”),它也不起作用。
FINAL SOLUTION: click on "Copy server URL" and paste it on your browser.最终解决方案:单击“复制服务器 URL”并将其粘贴到浏览器上。 It worked for me!
它对我有用!
EDIT: Copying server URL might not be necessary, as explained by Eric Mutta in the comment below.编辑:可能不需要复制服务器 URL,正如 Eric Mutta 在下面的评论中所解释的那样。
我不小心将 js 文件命名为.min
而不是.min.js
...
In my case, I was working on legacy code就我而言,我正在处理遗留代码
and I have this line of code我有这行代码
<script type="text/javascript" src="js/i18n.js.php"></script>
I was confused about how this supposed to work this code was calling PHP file not js我对这应该如何工作感到困惑,这段代码调用的是 PHP 文件而不是 js
despite it was working on the live server尽管它在实时服务器上工作
but I have this error on the stage sever但我在舞台服务器上有这个错误
and the content type was并且内容类型是
content-type: text/html; charset=UTF-8
even it is text/javascript in the script tag即使它是脚本标签中的text/javascript
and after I added在我添加之后
header('Content-Type: text/javascript');
at the beginning for file i18n.js.php在文件i18n.js.php的开头
the error is fixed错误已修复
Python flask蟒蛇烧瓶
On Windows , it uses data from the registry, so if the "Content Type" value in HKCR/.js is not set to the proper MIME type it can cause your problem.在Windows 上,它使用注册表中的数据,因此如果 HKCR/.js 中的“内容类型”值未设置为正确的 MIME 类型,则可能会导致您的问题。
Open regedit and go to the HKEY_CLASSES_ROOT make sure the key .js/Content Type has the value text/javascript打开regedit并转到HKEY_CLASSES_ROOT确保密钥.js/Content Type具有值text/javascript
C:\>reg query HKCR\.js /v "Content Type"
HKEY_CLASSES_ROOT\.js
Content Type REG_SZ text/javascript
In my case (React app), just force cleaning the cache and it worked.就我而言(React 应用程序),只需强制清理缓存即可。
In my case Spring Security was looking for authentication before allowing calls to external libraries.在我的例子中,Spring Security 在允许调用外部库之前正在寻找身份验证。 I had a folder /dist/.. that I had added to the project, once I added the folder to the ignore list in my WebSecurityConfig class, it worked fine.
我有一个文件夹 /dist/.. 已添加到项目中,一旦将文件夹添加到 WebSecurityConfig class 的忽略列表中,它就可以正常工作。
web.ignoring().antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**", "/error", "/dist/**");
src
in script
tag.script
标签中的空src
。 In my case, i was dynamically populating src
from script(php in my case), but in a particular case src
remained empty, which caused this error.就我而言,我是从脚本(在我的情况下为 php)动态填充
src
,但在特定情况下src
保持为空,这导致了此错误。 Out was something like this:出来是这样的:
<script src=""></script> //empty src causes error
So instead of empty src
in script
tag, I removed the script tag all together.因此,我没有将
script
标签中的空src
删除,而是一起删除了脚本标签。 Something like this:像这样的东西:
if($src !== ''){
echo '<script src="'.$src.'"></script>';
}
Add the code snippet as shown below to the entry html.将如下所示的代码片段添加到条目 html 中。 ie "index.html" in reactjs
即reactjs中的“index.html”
<div id="wrapper"></div> <base href="/" />
I had my web server returning:我让我的网络服务器返回:
Content-Type: application\javascript
and couldn't for the life of me figure out what was wrong.并且终其一生都无法弄清楚出了什么问题。 Then I realized I had the slash in the wrong direction.
然后我意识到我的斜线方向错误。 It should be:
应该是:
Content-Type: application/javascript
我的问题是我一直将 CSS 文件放在尝试检查页面中的文件位置末尾的脚本定义区域中
I am using SpringMVC+tomcat+React我正在使用 SpringMVC+tomcat+React
@Anfuca's answer does not work for me(force cleaning the browser's cache) @Anfuca 的回答对我不起作用(强制清理浏览器的缓存)
I used Filter to forward specific url pattern to the React's index.html
我使用过滤器将特定的 url 模式转发到 React 的
index.html
public class FrontFilter extends HttpFilter {
@Override
protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
boolean startsWithApi = requestURI.startsWith("/api/");
boolean isFrontendUri = requestURI.startsWith("/index.html");
if (!startsWithApi && !isFrontendUri) {
req.getRequestDispatcher("/index.html").forward(req, res);
}
super.doFilter(wrapped, res, chain);
}
}
There is no Spring Security problem bcs my filter executes before Spring Security's没有 Spring 安全问题 bcs 我的过滤器在 Spring 安全之前执行
but I still see the same error and find here但我仍然看到同样的错误并在这里找到
Then I realized that I forgot adding one more condition for JS and CSS:然后我意识到我忘了为 JS 和 CSS 添加一个条件:
boolean startsWithStatic = requestURI.startsWith(contextPath + "/static");
Add this to my if condition and problem solved, no more error with MIME type or ('text/html') with js and css将此添加到我的 if 条件和问题已解决,MIME 类型或 ('text/html') 与 js 和 css 没有更多错误
Root cause is that I incorrectly forward JS and CSS type to HTML type根本原因是我错误地将 JS 和 CSS 类型转发到 HTML 类型
In my case, while executing my typescript file, I wrote:就我而言,在执行我的 typescript 文件时,我写道:
<script src="./script.ts"></script>
Instead of:代替:
<script src="./script.js"></script>
I got the same error.我得到了同样的错误。 I realized my app.js was in another folder.
我意识到我的 app.js 在另一个文件夹中。 I just moved it into that folder where my index.html file is and it resolved.
我只是将它移到我的 index.html 文件所在的文件夹中,它已解决。
You can use just Use type您可以只使用 Use type
or which you are using you choose that file type或者你正在使用你选择那个文件类型
In my case I had a symlink for the 404'd file and my Tomcat was not configured to allow symlinks.在我的情况下,我有一个 404'd 文件的符号链接,我的 Tomcat 没有配置为允许符号链接。
I know that it is not likely to be the cause for most people, but if you are desperate, check this possibility just in case.我知道这对大多数人来说不太可能是原因,但是如果您绝望,请检查这种可能性以防万一。
If you have a route on express such as:如果您在快递上有一条路线,例如:
app.get("*", (req, res) => {
...
});
Try to change it for something more specific:尝试将其更改为更具体的内容:
app.get("/", (req, res) => {
...
});
For example.例如。
Or else you just might find yourself recursively serving the same HTML template over and over...否则,您可能会发现自己一遍又一遍地递归提供相同的 HTML 模板......
i had the same issue and the problem was that i was missing a slash in my path.我遇到了同样的问题,问题是我的路径中缺少一个斜线。
i had我有
and to fix it i only needed to add the slash in between jquery-ui and jquery-ui.min:并修复它我只需要在 jquery-ui 和 jquery-ui.min 之间添加斜线:
<script type="text/javascript" src="js/jquery-ui/jquery-ui.min.js"></script>
hope this helps :D希望这有帮助:D
I hade same problem then i fixed like this我有同样的问题,然后我就这样解决了
change "text/javascript"
to到
type="application/json"
I solved my problem by adding just ${pageContext.request.contextPath} to my jsp path .我通过将 ${pageContext.request.contextPath} 添加到我的 jsp path 解决了我的问题。 in stead of :
代替:
<script src="static/js/jquery-3.2.1.min.js"></script>
I set :我设置:
<script src="${pageContext.request.contextPath}/static/js/jquery-3.2.1.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.