Zach B
Zach B

Reputation: 534

"$" Undefined when using jQuery in Visual Studio

I am trying to learn how to get JSON data using jQuery (using Visual Studio) but am hitting a runtime error in VS.

Error message: JavaScript runtime error: '$' is undefined

It seems that VS is just not recognizing jQuery. Alternatively, I have tried referencing the external jQuery library via their CDN but it did not resolve it.

Anyone have an idea why I am running into the error?

HTML:

<!DOCTYPE html>
<html>
<head>

    <title>JSON Test</title>

    <link href="/css/default.css" rel="stylesheet" />
    <script src="/js/jquery-1.10.2.min"></script>
    <script src="/js/default.js"></script>

</head>

<body>
    <button onclick="canWeGetData()">Get Data!</button>
</body>

</html>

Default.JS:

function canWeGetData() {

var error = "Null data!";
var success = "Good data!";

$.getJSON("http://ip.jsontest.com/?callback=showMyIP", function (data) {

    if (data == null) {
        document.writeln(error);
    }
    else {
        document.writeln(success);
    }

});

};

Upvotes: 1

Views: 4556

Answers (2)

user2779544
user2779544

Reputation: 429

doubt jquery is not present

try

<script src="//code.jquery.com/jquery-1.9.1.js"></script>

Upvotes: 2

Linga
Linga

Reputation: 10573

Jquery isn't present.

Look at this line:

<script src="/js/jquery-1.10.2.min.js"></script> <!-- you missed the .js -->

Also, Check whether the path is correct. Or simply use

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">

Upvotes: 5

Related Questions