Javascript 资源被解释为脚本但使用 MIME 类型文本/纯文本传输 - 用于本地文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12003107/
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
Resource interpreted as Script but transferred with MIME type text/plain - for local file
提问by batzkoo
I'm getting a "Resource interpreted as Script but transferred with MIME type text/plain" warning in Google Chrome when including a local script file.
包含本地脚本文件时,我在 Google Chrome 中收到“资源被解释为脚本但使用 MIME 类型文本/纯文本传输”警告。
I know the problem appears when loading a file from a server or through ajax which most often depends on wrong headers being set.
我知道从服务器或通过 ajax 加载文件时会出现问题,这通常取决于设置的错误标头。
The weird thing is that I get this warning even though it is run from a local folder: file:///C:/test/foo.html
奇怪的是,即使它是从本地文件夹运行的,我也会收到此警告: file:///C:/test/foo.html
This happens only in Chrome with the most basic html there is:
这只发生在具有最基本 html 的 Chrome 中:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="bar.js"></script>
</head>
<body>
</body>
</html>
bar.js is also as simple as it can get:
bar.js 也非常简单:
function hello() {}
I've tried adding a meta tag:
我试过添加一个元标记:
<meta http-equiv="content-script-type" content="text/javascript">
and tested with other doctypes but nothing seems to help.
并使用其他文档类型进行了测试,但似乎没有任何帮助。
This obviously isn't a real issue since the scripts still work fine, but I'm working on a large project and currently have around 150 scripts included. It therefore makes it difficult to see when an actual warning occurs in between them.
这显然不是一个真正的问题,因为脚本仍然可以正常工作,但我正在处理一个大型项目,目前包含大约 150 个脚本。因此,很难看出它们之间何时发生实际警告。
Everything works fine when I run the file on a server, locally or remote.
当我在本地或远程服务器上运行文件时,一切正常。
Any ideas on why chrome is annoying me with this?
关于为什么 chrome 惹恼我的任何想法?
回答by Simon Sarris
I figured it out!
我想到了!
The Visual Studio installer must have added an errant line to the registry.
Visual Studio 安装程序必须在注册表中添加了错误行。
open up regedit
and take a look at this registry key:
打开regedit
并查看此注册表项:
See that key? The Content Type key? change its value from text/plain to text/javascript.
看到那个钥匙了吗?内容类型键?将其值从 text/plain 更改为 text/javascript。
Finally chrome can breathe easy again.
终于铬可以重新松口气了。
I should note that neither Content Type nor PercievedType are there by default on Windows 7, so you could probably safely delete them both, but the minimum you need to do is that edit.
我应该注意,Windows 7 上默认情况下既不存在 Content Type 也不 PercievedType,因此您可能可以安全地删除它们,但您需要做的最少的是编辑。
Anyway I hope this fixes it for you too!
无论如何,我希望这也能为您解决问题!
回答by user2383049
I tried fixing this problem using this method but it didn't work for me.
我尝试使用此方法解决此问题,但对我不起作用。
My problem was that IIS manager didn't have MIME types in HTTP Features.
我的问题是 IIS 管理器在 HTTP 功能中没有 MIME 类型。
I was able to turn it on by enabling Static Context via...
我能够通过启用静态上下文来打开它...
--> Control Panel
--> 控制面板
--> Programs
--> 程序
--> Turn Windows features on or off
--> 打开或关闭 Windows 功能
--> Internet Information Services
--> 互联网信息服务
--> World Wide Web Services
--> 万维网服务
--> Common HTTP features
--> 常见的 HTTP 特性
--> [X] Static Content.
--> [X] 静态内容。
After this, MIME types appeared and everything started working again.
此后,MIME 类型出现,一切又开始工作。
回答by Richard Wong
The accepted answer is a great one! However, just to post an answer for those who encounter problem like me, who use a department/college computer sometimes, where I do not have the permission to change any key value in regedit.
接受的答案是一个很好的答案!但是,只是为那些遇到问题的人发布一个答案,他们有时会使用部门/大学计算机,而我无权更改 regedit 中的任何键值。
Change
改变
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript" src="main.js"></script>
to
到
<script src="main.js"></script>
<script src="main.js"></script>
Although the error message still exist, the page loaded correctly.
尽管错误消息仍然存在,但页面加载正确。