377

I'm attempting to create an AJAX request. Here's my function definition:

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

Here's where I call it, providing the parameters:

AJAXrequest('voting.ajax.php', data, function(data) {
    // function body
});

Yet the callback does not run, and instead I get a console.error:

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

Why?

11
  • 2
    whether jquery is included Commented Aug 16, 2013 at 10:32
  • 4
    change this $.ajax() ({ to $.ajax({ Commented Aug 16, 2013 at 10:34
  • 4
    You called $.ajax without arguments ($.ajax()) and the return value is a jqXHR object, which is not a function. Hence $.ajax()(...) will throw an error. Commented Aug 16, 2013 at 10:34
  • 2
    either you missed to include jquery.js OR you have included jquery.js below the function call OR please try jQuery.ajax (replace $ with jQuery). Commented Aug 16, 2013 at 10:34
  • 137
    In my case, it's because I used slim minified version of JQuery which takes out ajax function Commented Dec 7, 2016 at 8:36

15 Answers 15

1396

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.

Sign up to request clarification or add additional context in comments.

15 Comments

This is what fixed my problem! What the heck jQuery?? Why is $.ajax removed from the "slim" build?
Slim build claimed another victim.
I copied the code from Bootstrap. They use the slim version. Maybe you too?
this answer is still useful in September 13 2020 haha! another up-vote from East Africa!
2022 and slim is still missing it
|
177

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"

1 Comment

Using the min build that includes Ajax will be a much better solution for most people, considering file size. See @Daniel Mendoza answer below
26

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.

7 Comments

It's not a syntax error. The return value was just expected to be a function which it is not.
Ahhh I see. So really, this problem is just a missing jQuery script file reference really.
No. Look at it as $.ajax()();. This is valid JavaScript. It means "call $.ajax and whatever it returns, call it as well". But $.ajax does not return a function (which could be called), it returns a jqXHR object, so it throws an error. Your answer is correct, the additional () are the problem, but the description of the problem is not correct (it's not a syntax error, well at least not for the parser).
No, sorry, that's what I was stating in my previous comment; that this is not a syntax error problem, since the syntax is valid, but rather an issue with the jQuery script file not loaded at the time $.ajax() is used.
No, jquery.js is not missing (if it was the error would be about the $) but it's not a syntax error either in that it's valid JS. It's just not appropriate syntax for the desired behaviour, so it causes a runtime error as explained by Felix.
|
11

Reference the jquery min version that includes ajax:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

Comments

8

I used following version of jQuery and it worked

<script src="https://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

Comments

6

Don't use the slim build of jQuery, which doesn't have the ajax function in it. Use the minified version instead from https://releases.jquery.com/

Note: Make sure to not use the jquery link copied from the Bootstrap website, because they use the slim version.

Comments

3

I know this is an old posting -- and Gus basically answered it. But in my experience, JQuery has since changed their code for importing from the CDN - so I thought I would go ahead and post their latest code for importing from the CDN:

<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>

Comments

3

If you are using bootstrap html template remember to remove the link to jquery slim at the bottom of the template.

Comments

2

There is a syntax error as you have placed parenthesis after ajax function and another set of parenthesis to define the argument list:-

As you have written:-

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

The parenthesis around ajax has to be removed it should be:-

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

Comments

2
  1. It may be because of the slim build of JQuery. Try to use 'uncompressed' or 'minified' builds. (JQuery CDNs)

  2. It also may be because of loading Bootstrap CDN before the JQuery CDN. In my case, I removed the Bootstrap CDN and move the JQuery one to the end of the 'body' tag and the problem was solved.

Comments

1

You have an error in your AJAX function, too much brackets, try instead $.ajax({

Comments

1

I encountered the same question, and my solution was: add the JQuery script.

Especially, we should make sure the corresponding JQuery is loaded when we debug our js under the firefox/chrome.

Comments

0

For anyone trying to run this in nodejs: It won't work out of the box, since jquery needs a browser (or similar)! I was just trying to get the import to run and was logging console.log($) which wrote [Function] and then also console.log($.ajax) which returned undefined. I had no tsc errors and had autocomplete from intellij, so I was wondering what's going on.

Then at some point I realised that node might be the problem and not typescript. I tried the same code in the browser and it worked. To make it work you need to run:

require("jsdom").env("", function(err, window) {
    if (err) {
        console.error(err);
        return;
    }

    var $ = require("jquery")(window);
});

(credits: https://stackoverflow.com/a/4129032/3022127)

Comments

0

I included bootstrap scripts and the first one(jquery.slim) is the reason of this error. Removing the first row solved my problem. error-img

Comments

-2

let me share my experience:

my html page designer use:

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>

when I create a simple AJAX request, getting an error it says, TypeError: $.ajax(…) is not a function

So, i add:

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

then, it perfectly works for me at least.

1 Comment

No. Loading jQuery slim and then immediately overwriting it with jQuery is utterly pointless and a waste of bandwidth.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.