8

I have this error on a simple jsp : Uncaught ReferenceError: $ is not defined

I've just try to recall a service rest on another project on eclipse but it seem doesn't work..

Code is here :

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <script rel="javascript" type="text/javascript" href="js/jquery-1.11.3.min.js" />

</head>

<body>
    <script>
        var people = {
            "address": "Street 12",
            "name": "twelve",
            "id": 12,
            "surname": "twelve"
        };

        function sendobject() {
            $.ajax({
                type: "POST",
                url: "http://localhost:8080/HibernateTutorialWeb/rest/person/post",
                data: markers,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    alert(data);
                },
                failure: function(errMsg) {
                    alert(errMsg);
                }
            });
        }
    </script>
    <input type="button" onclick="sendobject()" value="send"> </input>


</body>
</<html>

Update:

Tried using the jQuery from Google CDN, but still doesn't work

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

Uncaught ReferenceError: $ is not defined sendobject @ index.jsp:15onclick @ index.jsp:28


This question is not a duplicate of Uncaught ReferenceError: $ is not defined?

Because all the answer of that question suggest to put the references to the jquery scripts first, but it does not work for me .

The right solution was given in tushar's answer

So it's a similar question with a different problem and different solution.


Dwhitz
  • 1,250
  • 7
  • 26
  • 38
  • you should check if the jquery script is fully loaded in your browser: http://stackoverflow.com/questions/7486309/how-to-make-script-execution-wait-until-jquery-is-loaded – Pieter Willaert Nov 10 '15 at 09:23
  • 1
    Don't change the original code. Please add it as `Edit:` or `Update:` – Tushar Nov 10 '15 at 09:28
  • 1
    Possible duplicate of [Uncaught ReferenceError: $ is not defined?](https://stackoverflow.com/questions/2075337/uncaught-referenceerror-is-not-defined) – Moradnejad Jun 18 '19 at 10:59
  • 1
    @Moradnejad This question is not a duplicate of that, I've updated it. – Dwhitz Jun 18 '19 at 11:57

1 Answers1

15

<script> should not be self-closed, it'll not load the script. See Why don't self-closing script tags work?

Change

<script rel="javascript" type="text/javascript" href="js/jquery-1.11.3.min.js"/>

to

<script rel="javascript" type="text/javascript" href="js/jquery-1.11.3.min.js"></script>
Community
  • 1
  • 1
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • @Dhn Check if the file is correctly loaded. Also try using `jQuery` instead of `$`. – Tushar Nov 10 '15 at 09:26
  • As an addition, put scripts at the bottom of the page - the rendering begins sooner and defers connection limit(s). This avoids the "blank white screen" problem and hopefully enchances user experience. – urbz Nov 10 '15 at 09:26
  • I just put the script at the bottom, but error is still here – Dwhitz Nov 10 '15 at 09:29
  • @Dhn Can you please create demo on [jsfiddle](https://jsfiddle.net) with complete code – Tushar Nov 10 '15 at 09:30
  • yes, but it was useless because it call a project on my workspace.. so fiddle is here : https://jsfiddle.net/f0wte14g/1/ @Tushar – Dwhitz Nov 10 '15 at 09:32
  • 1
    @Dhn Check https://jsfiddle.net/tusharj/f0wte14g/2/. It doesn't throw `$ is not defined` error. Please check in your code is jQuery is loaded correctly. Also, the demo throws `marker is not defined` error as it is not added in the code snippet. – Tushar Nov 10 '15 at 09:34