TypeError: $.ajax(...) is not a function?

I'm trying to create a simple AJAX request which returns some data from a MySQL database. Here's my function below:

function AJAXrequest(url, postedData, callback) {
    $.ajax() ({
        type: 'POST',
        url: url,
        data: postedData,
        dataType: 'json',
        success: callback
    });
}

...and here's where I call it, parsing in the required parameters:

AJAXrequest('voting.ajax.php', imageData, function(data) {
    console.log("success!");
});

on the page voting.ajax.php , I'm currently just returning a JSON-encoded variable equal to 1 for testing:

$someNumber = 1;
return json_encode($someNumber);

Yet, my success callback does not run (as "success!" is not logged to the console), and I get an error in my console:

TypeError: $.ajax(...) is not a function.
success: callback

What does this mean? I've done AJAX requests before where the success event triggers an anonymous function inside of $.ajax, but now I'm trying to run a separate named function (in this case, a callback). How do I go about this?


Not sure, but it looks like you have a syntax error in your code. Try:

$.ajax({
  type: 'POST',
  url: url,
  data: postedData,
  dataType: 'json',
  success: callback
});

You had extra brackets next to $.ajax which were not needed. If you still get the error, then the jQuery script file is not loaded.


Neither of the answers here helped me. The problem was: I was using the slim build of jQuery , which had some things removed, ajax being one of them.

The solution: Just download the regular (compressed or not) version of jQuery here and include it in your project.


Double-check if you're using full-version of jquery and not some slim version.

I was using the jquery cdn-script link that comes with jquery. The problem is this one by default is slim.jquery.js which doesn't have the ajax function in it. So, if you're using (copy-pasted from Bootstrap website) slim version jquery script link, use the full version instead.

That is to say use <script src="https://code.jquery.com/jquery-3.1.1.min.js"> instead of <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"

链接地址: http://www.djcxy.com/p/51960.html

上一篇: 返回for循环中AJAX链中最后一次AJAX请求的值

下一篇: TypeError:$ .ajax(...)不是函数吗?