Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery CDN is not loading on LocalHost

I have a jquery cdn loading from the following:

<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?

like image 847
FluxEngine Avatar asked Dec 03 '22 23:12

FluxEngine


2 Answers

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.com which of course doesn't exist.

You should set up a server on your computer so that you'd have an actual localhost 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.

like image 73
JJJ Avatar answered Dec 19 '22 07:12

JJJ


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.

like image 30
Adam Clason Avatar answered Dec 19 '22 08:12

Adam Clason