28

I would like to create a JavaScript function which returns the value of a jQuery AJAX call. I would like something like this.

function checkUserIdExists(userid){
    return $.ajax({
        url: 'theurl',
        type: 'GET',
        cache: false,
        data: {
           userid: userid
        },
        success: function(data){
            return data;
        }
    });
}

I know I can do this by setting async to false but I would rather not.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
Timothy Ruhle
  • 7,387
  • 10
  • 41
  • 67

6 Answers6

32

You can't return data returned by an AJAX call unless you want to call it synchronously (and you don't – trust me). But what you can return is a promise of a data returned by an AJAX call and you can do it actually in a very elegant way.

(UPDATE: Please note that currently jQuery Promises are not compatible with the Promises/A+ specification - more info in this answer.)

Basically you can return the return value of your $.ajax(...) call:

function checkUserIdExists(userid){
    return $.ajax({
        url: 'theurl',
        type: 'GET',
        cache: false,
        data: {
           userid: userid
        }
    });
}

and someone who calls your function can use it like this:

checkUserIdExists(userid).success(function (data) {
    // do something with data
});

See this post of mine for a better explanation and demos if you are interested.

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
16

you can pass in a callback function:

function checkUserIdExists(userid, callback) {
    $.ajax({
        ...
        success: callback
    });
}

checkUserIdExists(4, function(data) {

});
Omer Bokhari
  • 57,458
  • 12
  • 44
  • 58
9

With jQuery 1.5, you can use the brand-new $.Deferred feature, which is meant for exactly this.

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.ajax({ url: "example.php" })
    .success(function() { alert("success"); })
    .error(function() { alert("error"); })
    .complete(function() { alert("complete"); });

// perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });

Source

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
8

As of jQuery 1.8, the "success", "error" and "complete" callback are deprecated. Instead you should be using "done", "fail" and "always".

So you could have:

function checkUserIdExists(userid, callback) {
        return $.ajax({
        url: 'theurl',
        type: 'GET',
        cache: false,
        data: {
           userid: userid
        }
    })
    .done(callback)
    .fail(function(jqXHR, textStatus, errorThrown) {
        // Handle error
    });
}

checkUserIdExists(2, function(data) {
    console.log(data); // Do what you want with the data returned
});
3

This isn't how JavaScript asynchronous programming was really meant to be done. Instead, use a callback in your success function to call another function to use your data returned from the server.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
2

Tim, the two scenarios are mutually exclusive; an asynchronous operation will not serve any purpose for, nor will it be able to retrieve returned data.

You should look at an event-enabled infrastructure for your ajax calls

Olaseni
  • 7,698
  • 16
  • 44
  • 68