Javascript “资源被解释为脚本,但使用 MIME 类型 text/html 传输。”

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

"Resource interpreted as script but transferred with MIME type text/html."

javascriptjquerymacossafarihttp-headers

提问by DixonD

Sorry if this is actual duplicate but I haven't managed to find answer for my problem.

对不起,如果这是实际重复,但我还没有设法找到我的问题的答案。

I load the script with jQuery's $.getScript. But it causes the following error:

我用 jQuery 的 $.getScript 加载脚本。但它会导致以下错误:

Resource interpreted as script but transferred with MIME type text/html.

资源被解释为脚本但使用 MIME 类型 text/html 传输。

The problem appears only in Safari under Mac OS

该问题仅在 Mac OS 下的 Safari 中出现

If to look on headers received from the server, they contain Content-Type:application/x-javascript, so I really don't understand what the problem is.

如果查看从服务器收到的标头,它们包含Content-Type:application/x-javascript,所以我真的不明白问题是什么。

回答by Ja?ck

Resource interpreted as script but transferred with MIME type text/html.

资源被解释为脚本但使用 MIME 类型 text/html 传输。

Technically that's not an error but a notice / warning and should not cause any issues in particular; if anything it's a good indication that some browsers may choose to ignore such a response.

从技术上讲,这不是错误,而是通知/警告,不应引起任何特别的问题;如果有的话,这是一个很好的迹象,表明某些浏览器可能会选择忽略此类响应。

The correct Content-Typeresponse header for JavaScript according to the RFCis:

Content-Type根据RFC,JavaScript的正确响应标头是:

Content-Type: application/javascript

Previous values of text/javascriptand application/x-javascriptare either obsolete or deprecated respectively; that said, IE <= 8 will only accept the former so be careful when making any changes to your documents.

的先前值text/javascriptapplication/x-javascript过时或分别弃用; 也就是说,IE <= 8 只会接受前者,因此在对文档进行任何更改时要小心。

回答by K. Weber

I had same issue and it was caused by Web.Configauthorization block, I had to add an entry in Web.Configto make this script available also when not logged in:

我遇到了同样的问题,它是由Web.Config授权块引起的,我必须在Web.Config 中添加一个条目才能使此脚本在未登录时也可用:

<location path="Scripts/jquery-2.0.3.min.js">
  <system.web>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>