javascript Chrome 显示“资源被解释为脚本,但使用 MIME 类型 text/plain 传输。”,没有服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16967976/
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-10-27 06:37:45  来源:igfitidea点击:

Chrome says “Resource interpreted as script but transferred with MIME type text/plain.”, without server

javascriptgoogle-chrome

提问by rengel

This problem even arises, when there is no server access whatsoever. index.html is just accessing some locally stored JavaScript file:

当没有任何服务器访问时,甚至会出现这个问题。index.html 只是访问一些本地存储的 JavaScript 文件:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <script src="timeline.js"></script>
</head>

<body>
   <p>The Body</p>
</body>
</html>

When displayed in the browser, Chrome says:

在浏览器中显示时,Chrome 显示:

Resource interpreted as Script but transferred with MIME type text/plain:  
file:///D:/Workspace/timeline/examples/engel-timeline/timelineReusable.v0.0/timeline.js". 

How can I suppress this message? (Adding a 'content-type' doesn't help!)

我怎样才能抑制这个消息?(添加“内容类型”无济于事!)

回答by Pluto

I believe the reason for this is that your script is being loaded locally from your computer. It's directly accessing the file, so there are no request headers here, meaning there will be no MIME type that comes with your JS file.

我相信这是因为您的脚本是从您的计算机本地加载的。它直接访问文件,所以这里没有请求头,这意味着你的 JS 文件不会有 MIME 类型。

If you upload your file on the Internet, then this will be fixed because it uses a different protocol (HTTP).

如果您在 Internet 上上传文件,则此问题将得到解决,因为它使用不同的协议 (HTTP)。

This problem should only end up happening when there is no server. If there is a server, then make sure you're using the server's URL, like http://localhost:80/timeline.js. And if that still is giving you a problem, then your server's MIME types need to be configured (though it's just a JavaScript file, so that shouldn't be a problem).

这个问题应该只在没有服务器时才会发生。如果有服务器,请确保您使用的是服务器的 URL,例如http://localhost:80/timeline.js. 如果这仍然给您带来问题,那么您的服务器的 MIME 类型需要配置(尽管它只是一个 JavaScript 文件,所以这应该不是问题)。

回答by mishik

Try opening regedit: HKEY_CLASSES_ROOT\.js and either change or add string "Content Type" with value "text/javascript"

尝试打开 regedit: HKEY_CLASSES_ROOT\.js 并更改或添加值为“text/javascript”的字符串“Content Type”

回答by Donkzilla

The error also arises in this situation:

在这种情况下也会出现错误:

<script type="text/javascript" src="/livehelp/livehelp_js.php>

indicating that Chrome is the problem, the code is good. Chrome is wrongly blocking a script sent in a php file. The script is not blocked in other browsers except IE 11 and only when Apache directive Header set X-Content-Type-Options: "nosniff"is set.

说明 Chrome 有问题,代码很好。Chrome 错误地阻止了在 php 文件中发送的脚本。该脚本不会在除 IE 11 之外的其他浏览器中被阻止,并且仅在Header set X-Content-Type-Options: "nosniff"设置了Apache 指令时才被阻止。