javascript jQuery CDN 未在 LocalHost 上加载

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

jQuery CDN is not loading on LocalHost

javascriptjquerycdn

提问by FluxEngine

I have a jquery cdn loading from the following:

我有一个 jquery cdn 从以下加载:

<head>
.
.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

then in the body I have my source for my script

然后在正文中我有我的脚本的来源

<body>
.
.<script src="app.js"></script>
</body>

This is all on local, but when I view in browser I keep getting the following errors in console:

这一切都在本地,但是当我在浏览器中查看时,我不断在控制台中收到以下错误:

GET file://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
Uncaught ReferenceError: $ is not defined 

I assume this is saying the jQuery function "$..." is undefined because there is an error in GET of the CDN, why would this be happening on local?

我认为这是说 jQuery 函数“$...”未定义,因为 CDN 的 GET 中存在错误,为什么会在本地发生这种情况?

回答by JJJ

You aren't actually running on localhost (http://localhost) but on the local file system (file:///path/to/whatever.html). The protocol is copied with the //link to file://ajax.googleapis.comwhich of course doesn't exist.

您实际上不是在 localhost ( http://localhost) 上运行,而是在本地文件系统 ( file:///path/to/whatever.html) 上运行。该协议与当然不存在的//链接一起复制file://ajax.googleapis.com

You should set up a server on your computer so that you'd have an actuallocalhost accessed with the http protocol. It would have other benefits as well since browsers act a bit differently also in other regards when the page is loaded directly from the file system.

您应该在您的计算机上设置一个服务器,以便您可以使用 http 协议访问一个实际的本地主机。它还有其他好处,因为当页面直接从文件系统加载时,浏览器在其他方面的行为也会有所不同。

回答by Adam Clason

Have you tried removing the "//" at the beginning of the "src" attribute and instead using "http://"? It is probably appending "localhost" to the beginning of the URL because of those slashes.

您是否尝试删除“src”属性开头的“//”而使用“http://”?由于这些斜杠,它可能将“localhost”附加到 URL 的开头。

回答by Googol

I've answered a similar question in How to use Bootstrap CDN?and, as Juhana said, the problem is the missing protocol when loading jQuery.

我在如何使用 Bootstrap CDN 中回答了类似的问题而且,正如 Juhana 所说,问题在于加载 jQuery 时缺少协议。

The protocol-less CDN works when you are using http or https, because browsers will add the prefix to the jQuery URL with it.

当您使用 http 或 https 时,无协议 CDN 有效,因为浏览器会将前缀添加到 jQuery URL 中。

But when using a local html file, the missing protocol will be file:and browsers will not be able to find jQuery in something like file://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.jsfailing to load it. In that case you must add the protocol manually, which is a pitty if you finally use the page on-line because a protocol-less URL is better and more cache friendly (http://encosia.com/cripple-the-google-cdns-caching-with-a-single-character/).

但是当使用本地 html 文件时,缺少的协议将是file:浏览器将无法在无法file://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js加载它的情况下找到 jQuery 。在这种情况下,您必须手动添加协议,如果您最终在线使用该页面,这是一个遗憾,因为无协议 URL 更好,缓存更友好(http://encosia.com/cripple-the-google- cdns-caching-with-a-single-character/)。