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. -_-"