资源解释为脚本,但传输 MIME 类型的文本/纯文本本地文件

我得到了一个“资源解释为脚本,但传输与 MIME 类型的文本/普通”警告在谷歌浏览器中,当包含一个本地脚本文件。

我知道当从服务器或通过 ajax 加载文件时出现问题,而 Ajax 通常依赖于设置错误的头。

奇怪的是,即使这个警告是从本地文件夹 file:///C:/test/foo.html运行的,我还是会收到它

这种情况只有在最基本的 html 浏览器中才会发生:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="bar.js"></script>
</head>
<body>
</body>
</html>

Bar.js 也非常简单:

function hello() {}

我试过添加一个 meta 标签:

<meta http-equiv="content-script-type" content="text/javascript">

并与其他医学类型测试,但似乎没有什么帮助。

这显然不是一个真正的问题,因为脚本仍然工作良好,但我的工作在一个大型项目,目前有大约150个脚本包括在内。因此,很难看到它们之间何时发生实际的警告。

当我在本地或远程服务器上运行文件时,一切都很正常。

知道为什么 chrome 这么烦我吗?

88761 次浏览

I figured it out!

The Visual Studio installer must have added an errant line to the registry.

open up regedit and take a look at this registry key:

enter image description here

See that key? The Content Type key? change its value from text/plain to 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.

Anyway I hope this fixes it for you too!

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.

I was able to turn it on by enabling Static Context via...

--> Control Panel

--> Programs

--> Turn Windows features on or off

--> Internet Information Services

--> World Wide Web Services

--> Common HTTP features

--> [X] Static Content.

After this, MIME types appeared and everything started working again.

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.

Change

<script type="text/javascript" src="main.js"></script>

to

<script src="main.js"></script>

Although the error message still exist, the page loaded correctly.