Search code examples
javascriptpythonhtmldjangodjango-views

Uncaught TypeError: $.ajax is not a function in jQuery


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!


Solution

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