Javascript 禁用 Chrome 严格的 MIME 类型检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39228657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:20:57  来源:igfitidea点击:

Disable Chrome strict MIME type checking

javascriptjqueryangularjsgoogle-chrome

提问by Atul Sharma

Is there any way to disable strict MIME type checkingin Chrome.

有什么办法可以strict MIME type checking在 Chrome 中禁用。

Actually I'm making a JSONP request on cross domain. Its working fine on Firefox but, while using chrome its giving some error in console.

实际上,我正在跨域发出 JSONP 请求。它在 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

采纳答案by Gregtheitroade

The server should respond with the correct MIME Type for JSONP application/javascriptand your request should tell jQuery you are loading JSONP dataType: 'jsonp'

服务器应该以正确的 JSONP MIME 类型响应,application/javascript并且您的请求应该告诉 jQuery 您正在加载 JSONPdataType: 'jsonp'

Please see this answerfor further details ! You can also have a look a this oneas it explains why loading .jsfile with text/plainwon't work.

请参阅此答案以获取更多详细信息!你也可以看看这个,因为它解释了为什么加载.js文件text/plain不起作用。

回答by kujiy

In my case, I turned off X-Content-Type-Optionson nginxthen works fine. But make sure this declines your security level a little. Would be a temporally fix.

就我而言,我关掉了X-Content-Type-Optionsnginx再正常工作。但请确保这会稍微降低您的安全级别。将是一个暂时的修复。

# 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>

回答by Fresher

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

如果您无法解决问题,您可以在命令行上运行以下命令
chrome.exe --user-data-dir="C://Chrome dev session" --disable-web-security

Note: you have to navigate to the installation path of your chrome.
For example:cd C:\Program Files\Google\Chrome\Application

注意:您必须导航到 chrome 的安装路径。
例如: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.
I hope this should be helpful

将打开开发者会话 chrome 浏览器,您现在可以在新的 chrome 浏览器上启动您的应用程序。
我希望这会有所帮助

回答by Ozesh

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 i.eWin + R > regedit
  2. Head over to HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.js
  3. Check to if the Content Type is application/javascriptor not
  4. If not, then change it to application/javascriptand try again
  1. 打开注册表编辑器Win + R > regedit
  2. 前往 HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.js
  3. 检查如果内容类型是application/javascript或不是
  4. 如果不是,则将其更改为application/javascript并重试

回答by kujiy

Another solution when a file pretends another extension

当文件假装另一个扩展名时的另一种解决方案

I use phpinside of var.jsfile with this .htaccess.

phpvar.js文件里面用这个.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-Typeheader line in the .js(but php)file.

当我在 Chrome 上收到 MIME 类型警告时,我通过Content-Type.js(but php)文件中添加标题行来修复它。

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

A browser won't execute .jsfile because apache sends the Content-Typeheader of the file as application/x-httpd-phpthat is defined in .htaccess. That's a security reason. But apache won't execute php as far as htaccesscommands the impersonation, it's necessary. So we need to overwrite apache's Content-Typeheader with the php function header(). I guess that apache stops sending its own header when php sends it instead of apache before.

浏览器将不执行.js因为Apache发送文件Content-Type作为文件的标头application/x-httpd-php,其中所定义.htaccess。这是一个安全原因。但是 apache 不会执行 php 就htaccess命令模拟而言,这是必要的。所以我们需要Content-Type用php函数覆盖apache的头文件header()。我猜当 php 发送它而不是之前的 apache 时,apache 停止发送它自己的标头。