ChatGPT解决这个技术问题 Extra ChatGPT

Resource interpreted as Script but transferred with MIME type text/plain - for local file

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.

I know the problem appears when loading a file from a server or through ajax which most often depends on wrong headers being set.

The weird thing is that I get this warning even though it is run from a local folder: file:///C:/test/foo.html

This happens only in Chrome with the most basic html there is:

<!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:

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.

Everything works fine when I run the file on a server, locally or remote.

Any ideas on why chrome is annoying me with this?

you didn't happen to install the latest Visual Studio the other day, did you? This started happening to me and thats the only thing that changed...
yes, VS Express 2012 RC a few weeks ago. I thought about that too so I created the files with notepad++ instead but the problem remains the same. If VS is causing this then they must have changed some global setting/parameter? Question is what, and how do I change it back?
I'm still trying to figure out the same thing. It appears to be a global thing and it happened precisely after I installed VS2012. I'll let you know if I figure anything out. To be clear, I don't actually use Visual Studio for file editing, which makes it stranger.

S
Simon Sarris

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:

https://i.stack.imgur.com/PLamg.png

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!


Well done you! for finding the solution to this annoyance. I didn't delete the key but instead changed the data value to text/javascript. Thanks!
haha I changed it to text/script and it didn't work, maybe I should have tried text/javascript!
Thanks for the Solution ! I had the same problem... Actually with MIME type plain/text for me the javascript code had thrown errors. I had some strings (in an array) containing letters found in UTF-8 encoding seen in east-europian languages like "ő". "Gergő" is nice comon Hungarian first name. The errors also has gone thanks god, since I could not run my code - but I did not investigate this further.
Wow, how did you discover this solution?
@Pakman I had the problem one day, then I bought a new computer and didn't have the problem. Then, when I installed Visual Studio 2012 I noticed that it re-appeared, so I figured that was the culprit. I figured it changed something in the file associations of the registry because all my .js files had .txt looking icons, so I went to make sure they were associated with the right data type. They weren't!
e
emragins

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.


R
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.

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.


But OP's problem is that the page loads correctly, but he still gets an error message. This doesn't seem to change anything.