简体   繁体   English

禁用 Chrome 严格的 MIME 类型检查

[英]Disable Chrome strict MIME type checking

Is there any way to disable strict MIME type checking in Chrome.有什么方法可以在 Chrome 中禁用strict MIME type checking

Actually I'm making a JSONP request on cross domain.实际上,我正在跨域发出 JSONP 请求。 Its working fine on Firefox but, while using chrome its giving some error in console.它在 Firefox 上运行良好,但是,在使用 chrome 时,它​​在控制台中出现了一些错误。

Refused to execute script from ' https://example.com ' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.拒绝执行来自“ https://example.com ”的脚本,因为其 MIME 类型(“text/plain”)不可执行,并且启用了严格的 MIME 类型检查。

Its working perfectly in Mozilla.. Issue is arising in chrome only它在 Mozilla 中完美运行.. 问题仅在 chrome 中出现

Here are the response Headers of the request..这是请求的响应标头..

Cache-Control:no-cache, no-store
Connection:Keep-Alive
Content-Length:29303
Content-Type:text/plain;charset=ISO-8859-1
Date: xxxx
Expires:-1
Keep-Alive:timeout=5
max-age:Thu, 01 Jan 1970 00:00:00 GMT
pragma:no-cache
Set-Cookie:xxxx
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN

Workaround what i think : Externally setting content-type to application/javascript我认为的解决方法:从外部将内容类型设置为application/javascript

The server should respond with the correct MIME Type for JSONP application/javascript and your request should tell jQuery you are loading JSONP dataType: 'jsonp'服务器应该使用 JSONP application/javascript的正确 MIME 类型进行响应,并且您的请求应该告诉 jQuery 您正在加载 JSONP dataType: 'jsonp'

Please see this answer for further details !请参阅此答案以获取更多详细信息! You can also have a look a this one as it explains why loading .js file with text/plain won't work.你也可以看看这个,因为它解释了为什么用text/plain加载.js文件不起作用。

In my case, I turned off X-Content-Type-Options on nginx then works fine.就我而言,我关闭了nginx上的X-Content-Type-Options然后工作正常。 But make sure this declines your security level a little.但请确保这会稍微降低您的安全级别。 Would be a temporally fix.将是一个暂时的修复。

# Not work
add_header X-Content-Type-Options nosniff;
# OK (comment out)
#add_header X-Content-Type-Options nosniff;

It'll be the same for apache. apache 也一样。

<IfModule mod_headers.c>
  #Header set X-Content-Type-Options nosniff
</IfModule>

For Windows Users :对于 Windows 用户:

If this issue occurs on your self hosted server ( eg: your custom CDN) and the browser (Chrome) says something like ... ('text/plain') is not executable ... when trying to load your javascript file ...如果此问题发生在您的自托管服务器(例如:您的自定义 CDN)上,并且浏览器 (Chrome) 显示类似... ('text/plain') is not executable ...在尝试加载您的 javascript 文件时.. .

Here is what you need to do :这是你需要做的:

  1. Open the Registry Editor ie Win + R > regedit打开注册表编辑器,Win + R > regedit
  2. Head over to HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\.js前往HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\.js
  3. Check to if the Content Type is application/javascript or not检查内容类型是否为application/javascript
  4. If not, then change it to application/javascript and try again如果没有,则将其更改为application/javascript

also had same problem once,也有过同样的问题,

if you are unable to solve the problem you can run the following command on command line如果您无法解决问题,您可以在命令行上运行以下命令
chrome.exe --user-data-dir="C://Chrome dev session" --disable-web-security

Note: you have to navigate to the installation path of your chrome.注意:您必须导航到 chrome 的安装路径。
For example: cd C:\\Program Files\\Google\\Chrome\\Application例如: cd C:\\Program Files\\Google\\Chrome\\Application

A developer session chrome browser will be opened, you can now launch your app on the new chrome browse.将打开开发者会话 chrome 浏览器,您现在可以在新的 chrome 浏览器上启动您的应用程序。
I hope this should be helpful我希望这会有所帮助

Another solution when a file pretends another extension当文件假装另一个扩展名时的另一种解决方案

I use php inside of var.js file with this .htaccess .我在var.js文件中使用php和这个.htaccess

<Files var.js>
    AddType application/x-httpd-php .js
</Files>

Then I write php code in the .js file然后我在 .js 文件中编写 php 代码

<?php
// This is a `.js` file but works with php
echo "var js_variable = '$php_variable';";

When I got the MIME type warning on Chrome, I fixed it by adding a Content-Type header line in the .js(but php) file.当我在 Chrome 上收到 MIME 类型警告时,我通过在.js(but php)文件中添加Content-Type标题行来修复它。

<?php
header('Content-Type: application/javascript');        // <- Add this line
// This is a `.js` file but works with php
...

A browser won't execute .js file because apache sends the Content-Type header of the file as application/x-httpd-php that is defined in .htaccess .浏览器不会执行.js文件,因为 apache 将文件的Content-Type标头作为在.htaccess定义的application/x-httpd-php That's a security reason.这是一个安全原因。 But apache won't execute php as far as htaccess commands the impersonation, it's necessary.但是 apache 不会执行 php 就htaccess命令模拟而言,这是必要的。 So we need to overwrite apache's Content-Type header with the php function header() .所以我们需要用 php 函数header()覆盖 apache 的Content-Type头。 I guess that apache stops sending its own header when php sends it instead of apache before.我猜当 php 发送它而不是之前的 apache 时,apache 停止发送它自己的标头。

In case you are using node.js (with express)如果您使用的是node.js(带有 express)

If you want to serve static files in node.js, you need to use a function.如果你想在 node.js 中提供静态文件,你需要使用一个函数。 Add the following code to your js file:将以下代码添加到您的 js 文件中:

app.use(express.static("public"));

Where app is:应用程序在哪里:

const express = require("express");
const app = express();

Then create a folder called public in you project folder.然后在您的项目文件夹中创建一个名为 public 的文件夹。 (You could call it something else, this is just good practice but remember to change it from the function as well.) (您可以将其称为其他名称,这只是一个很好的做法,但请记住也要从函数中更改它。)

Then in this file create another folder named css (and/or images file under css if you want to serve static images as well.) then add your css files to this folder.然后在此文件中创建另一个名为 css 的文件夹(和/或 css 下的图像文件,如果您还想提供静态图像。)然后将您的 css 文件添加到此文件夹中。

After you add them change the stylesheet accordingly.添加它们后,相应地更改样式表。 For example if it was:例如,如果它是:

href="cssFileName.css"

and

src="imgName.png"

Make them:让他们:

href="css/cssFileName.css"
src="css/images/imgName.png"

That should work👌🏽这应该有效👌🏽

在 node.js 的 get 处理程序中将路由名称更改为单个路由我的 app.get('/all-produce/search',(req,res)) 不起作用,但我将其更改为 app.get('/getProduce ',(req,res)) 和与 html 链接的 CSS 文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MIME 类型 ('application/json') 不可执行,并且启用了严格的 MIME 类型检查 - MIME type ('application/json') is not executable, and strict MIME type checking is enabled 拒绝执行脚本并启用严格的 MIME 类型检查 - Refused to execute script and strict MIME type checking is enabled 拒绝执行脚本,是否启用了严格的 MIME 类型检查? - Refused to execute script, strict MIME type checking is enabled? 根据 HTML 规范对模块脚本强制执行严格的 MIME 类型检查 - Strict MIME type checking is enforced for module scripts per HTML spec 错误:MIME类型(&#39;application / json&#39;)无法执行,并且使用YQL语句启用了严格的MIME类型检查? - Error: MIME type ('application/json') is not executable, and strict MIME type checking is enabled with YQL statements? javascript:MIME类型(&#39;text / html&#39;)无法执行,并且启用了严格的MIME类型检查 - javascript: MIME type ('text/html') is not executable, and strict MIME type checking is enabled 加载模块脚本失败:服务器以非 JavaScript MIME 类型“”响应。 强制执行严格的 MIME 类型检查 - Failed to load module script: The server responded with a non-JavaScript MIME type of “”. Strict MIME type checking is enforced Angular 6:拒绝从“URL”执行脚本,因为它的 MIME 类型()不可执行,并且启用了严格的 MIME 类型检查 - Angular 6 : Refused to execute script from 'URL' because its MIME type( ) is not executable, & strict MIME type checking is enabled 拒绝执行脚本,因为它的 MIME 类型 ('application/gzip') 不可执行,并且启用了严格的 MIME 类型检查 - Refused to execute script because its MIME type ('application/gzip') is not executable, and strict MIME type checking is enabled 拒绝执行脚本,因为 MIME 类型('text/html')不可执行,并且启用了严格的 MIME 类型检查 - Refused to execute script because MIME type ('text/html') is not executable, and strict MIME type checking is enabled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM