Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
158
rated 0 times [  163] [ 5]  / answers: 1 / hits: 60734  / 15 Years ago, tue, august 4, 2009, 12:00:00

I use prototype to do my AJAX development, and I use the code like this:



somefunction: function(){
var result = ;
myAjax = new Ajax.Request(postUrl, {
method: 'post',
postBody: postData,
contentType: 'application/x-www-form-urlencoded',
onComplete: function(transport){
if (200 == transport.status) {
result = transport.responseText;
}
}
});
return result;
}


And I find that the result is an empty string. So, I tried this:



somefunction: function(){
var result = ;
myAjax = new Ajax.Request(postUrl, {
method: 'post',
postBody: postData,
contentType: 'application/x-www-form-urlencoded',
onComplete: function(transport){
if (200 == transport.status) {
result = transport.responseText;
return result;
}
}
});

}


But it didn't work also. How can I get the responseText for other method to use?


More From » ajax

 Answers
13

remember that onComplete is called long after the someFunction is done working. What you need to do is pass a callback function to the somefunction as a parameter. This function will be called when the process is done working (ie, onComplete):



somefunction: function(callback){
var result = ;
myAjax = new Ajax.Request(postUrl, {
method: 'post',
postBody: postData,
contentType: 'application/x-www-form-urlencoded',
onComplete: function(transport){
if (200 == transport.status) {
result = transport.responseText;
callback(result);
}
}
});

}
somefunction(function(result){
alert(result);
});

[#98994] Friday, July 31, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aleighabayleef

Total Points: 511
Total Questions: 99
Total Answers: 99

Location: Aruba
Member since Fri, Jun 24, 2022
2 Years ago
;