Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  84] [ 6]  / answers: 1 / hits: 106175  / 14 Years ago, tue, march 1, 2011, 12:00:00

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.


More From » ajax

 Answers
30

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


[#93523] Sunday, February 27, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janjadonb

Total Points: 4
Total Questions: 114
Total Answers: 118

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
janjadonb questions
;