jQuery 适用于本地主机,但不适用于网站

发布于 2024-08-31 06:42:54 字数 443 浏览 2 评论 0原文

使用 Notepad++ 和 XAMPP 来测试内容,然后通过 FTP 将其上传到网站。在我的代码的头部,我用它来调用 jQuery 和脚本:

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

它在我的 XAMPP 服务器上本地运行良好。但一旦将其上传到主机,就会抛出此错误:

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

不确定发生了什么,或者为什么使用 jQuery 遇到困难。有什么想法吗?

Using Notepad++ and XAMPP to test stuff, then I upload it to the website over FTP. In the head of my code, I have this to call jQuery and the script:

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

and it works fine locally on my XAMPP server. But as soon as it's uploaded onto the host, it throws this error:

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

Not sure what's going on, or why it's having a hard time using jQuery. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我家小可爱 2024-09-07 06:42:54

这根本不是 jQuery 特有的。当您从服务器加载页面时,系统会向 Web 服务器发送下载 /jquery.js 的请求。在服务器的响应中,有一个 HTTP 标头,它向客户端提供有关文件类型的提示。这称为 Content-Type 标头。上面的错误消息表明服务器(错误地)报告 /jquery.js 的类型为 text/html,但事实并非如此。

您必须修改远程 Web 服务器的配置,以确保扩展名为 .js 的文件正确报告为具有 application/x-javascript 作为其 MIME 类型,而不是 <代码>文本/html。这可以通过 Apache 配置文件中的以下指令来完成:

AddType application/x-javascript .js

不过,我不确定为什么您的其他 .js 文件可以工作。无论如何,如果您因任何原因无法修改网络服务器配置,您可以尝试使用外部托管的副本改为 jQuery

如果您在 Mac OS X 上使用 Safari,也可能会遇到 WebKit/Safari 中的错误。请参阅 此相关问题

This is not specific to jQuery at all. When you load your page from the server, a request is sent to the web server to download /jquery.js. In the server's response, there is a HTTP header which gives a hint about the type of the file to the client. This is called the Content-Type header. The above error message says that the server reports (incorrectly) that /jquery.js is of type text/html, while it is not.

You have to modify the configuration of the remote web server to ensure that files with .js extension are properly reported as having application/x-javascript as their MIME type instead of text/html. This can be done by the following directive in an Apache configuration file:

AddType application/x-javascript .js

I'm not sure why your other .js file works, though. Anyway, if you cannot modify the web server config for any reason, you can try using an externally hosted copy of jQuery instead.

It might have also happened that you hit a bug in WebKit/Safari if you are using Safari on Mac OS X. See this related question.

长途伴 2024-09-07 06:42:54

原来文件名“jQuery.js”把它弄乱了。在本地,大写“U”不是问题,但在服务器上它是导致它崩溃的原因。我什至不知道这会发生:-/

Turns out the file name "jQuery.js" is what messed it up. Locally, the uppercase 'U' wasn't an issue, but on the server it's what caused it to break. I didn't even know this could happen :-/

喜爱皱眉﹌ 2024-09-07 06:42:54

这意味着您的服务器未配置为对 js 文件使用正确的内容类型。它将它们作为 text/html 而不是 text/javascript 发送。可能服务器配置不正确。

It means your server isn't configured to use the correct content-type for js files. It sends them as text/html instead of text/javascript. Probablyy the server aint configured right.

坦然微笑 2024-09-07 06:42:54

为什么不使用谷歌托管的版本,很多人可能已经缓存了它并且始终保持更新等等。

http://code.google.com/apis/ajaxlibs/

Why not use the google hosted version which a lot of people will probably already have cached and it always stays updated, etc..

http://code.google.com/apis/ajaxlibs/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文