13

i have a problem with a little script on my html page. It's just a function which should be called by an onclick method in an a tag. It dont works. Everytime i click on the link in the browser the console says "ReferenceError: $ is not defined" and points on the third line of the code below.

<script>
    function del(urlToDelete) {
        $.ajax({
            url: urlToDelete,
            type: 'DELETE',
            success: function(results) {
                location.reload();
            }
        });
    }
</script>
moritz.muecke
  • 167
  • 1
  • 1
  • 5
  • Where is the code that you expect to define `$`? – Quentin Oct 09 '14 at 08:35
  • 1
    `$.ajax()` is a [jQuery](http://jquery.com) function, the `$` being a shorthand for `jQuery` when writing code using it, so you'd need to load jQuery before that code will work. From the error message you haven't done that. – Anthony Grist Oct 09 '14 at 08:37
  • include jquery properly – Govind Singh Oct 09 '14 at 08:38
  • 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

4 Answers4

33

You need to include jquery library for that.Like this. You need to include this 1st, then write $.ajax to execute.

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

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
2

It looks like the jQuery libraries have not been included within your project.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
LiamWilson94
  • 458
  • 2
  • 7
  • 26
0

The above answers use http version,which might not be allowed at production ,alternatively use:

<script
          src="https://code.jquery.com/jquery-3.4.1.min.js"
          integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
          crossorigin="anonymous"></script>

or visit: https://code.jquery.com/

nofoobar
  • 2,826
  • 20
  • 24
  • Errors : SCRIPT438: SCRIPT438: Object doesn't support property or method 'ajax' and SCRIPT5008: SCRIPT5008: Invalid left-hand side in assignment can u tell me after pasting this library in my html file it is diplaying the above errors – Pavana Sri Palepu May 06 '20 at 13:40
0

When pulling sample code from Bootstrap (and possibly others), it's likely the slim version. This doesn't include ajax. Pick "uncompressed" or "minified" from https://releases.jquery.com/jquery/.

Tyler Montney
  • 1,402
  • 1
  • 17
  • 25