skip to Main Content

I’m encountering an issue with jQuery’s $.ajax function in my web application. When I try to make an AJAX request using $.ajax, I’m getting the error "Uncaught TypeError: $.ajax is not a function" in the browser console.

Here’s the relevant code snippet:

$.ajax({
    url: "/add-to-wishlist",
    data: {"id": product_id},
    dataType: "json",
    beforeSend: function(){
        console.log("Adding to wishlist");
    },
    success: function(response){
        this_val.html("👍🏼");
        if (response.bool === true) {
            console.log("Added to wishlist");
        }
    },
    error: function(xhr, errmsg, err){
        console.log(xhr.status + ": " + xhr.responseText);
        console.log(errmsg);
    }
});

I’ve ensured that jQuery is properly included in my HTML file before including my JavaScript file. However, I’m still encountering this error.

Could anyone help me understand why I’m getting this error and how to resolve it? Any insights or suggestions would be greatly appreciated. Thank you!

2

Answers


  1. It appears like jQuery is not being used in your web project. These are the two typical methods for doing so:

    1. Download from Official Library: You can download jQuery directly from the official website and include it in your project. You can download it from jQuery’s official website Click .

    2. Using CDN (Content Delivery Network): Alternatively, you can include jQuery via a CDN link directly in your HTML file. Here’s an example of how you can include jQuery from the Google CDN:

        <script src="https://code.jquery.com/jquery-3.1.1.min.js"> </script>
    Login or Signup to reply.
  2. Sounds like jQuery isn’t correctly initialized when your script runs, or you’re using a slim version of jQuery which doesn’t include the $.ajax function. Double-check the version of jQuery you’re including; make sure it’s not the slim version. Also, ensure jQuery is loaded before your script tries to use $.ajax. You can verify it by logging jQuery or $ in the console before your AJAX call. If it’s undefined, that’s your cue to recheck the script tags’ order in your html.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search