1

Possible Duplicate:
How to return the response from an AJAX call from a function?

I have tried debugging in Google Chrome's Developer Console but in any way I cant get the result I want. I want to create a javascript function that will return a value from an external file through AJAX. I have tried this script:

var site = {
    token: function(){
        $.get('auth.php',function(data){
            return data;
        });
    }
};

...when I call the site.token() function it should return the fetched contents from auth.php. But it just doesn't. It is always undefined. I also tried this:

var site = {
    token: function(){
        var token = null;
        $.get('auth.php',function(data){
            token = data;
        });
        return token;
    }
};

But its still the same. I also find a work around by doing this:

var token = $.get('auth.php',function(data){
    return data;
});
token.responseText;

With that codes, I can now get my expected result. But when I put it already in a function:

var site = {
    token: function(){
        var token = $.get('auth.php',function(data){
            return data;
        });
        return token.responseText;
    }
};

...it fails again.

*P.S.: I don't know maybe I'm just really stressed in programming and why I can't do it right. -_-"

Community
  • 1
  • 1
Jhon Andrew
  • 188
  • 3
  • 14

3 Answers3

2

$.post() is short-hand for $.ajax(), and both are asynchronous (unless you do very bad things). You don't want to freeze the browser's GUI thread for the duration of the request to the php page. Thus site.token() can not reply with the data.

However, $.ajax() does return a promise (see http://www.erichynds.com/jquery/using-deferreds-in-jquery/ and how does jquery's promise method really work?). In essence, it says "I promise I'll call you back when I'm done." So, you can construct the class like this:

var site = {
    token: function(){
        var promise = $.get('auth.php',function(data){
            return data;
        });
        return promise;
    }
};

and call it like this:

var promise = site.token();
promise.done(function (data) {
    alert('got the data');
    console.dir(data);
});
Community
  • 1
  • 1
robrich
  • 13,017
  • 7
  • 36
  • 63
1

If this were possible, then the function idea could be discarded, and the syntax could just be:

var data = $.post("url");

It isn't possible however, because AJAX is asynchronous. It takes a while before the response is received. When this is the case, your function is called. That's the idea of passing a function.

There is no way to make this synchronous so as to use return. You can use $.ajax with async: false but it's going to be deprecated. The best you can do is return $.post(...) and then use .done(...) on the result to use the response.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
0

Ajax calls are asynchronous, thus you use your data before it is fetched. Consider using deffered object. http://api.jquery.com/category/deferred-object/

Hayk
  • 109
  • 6