Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return AJAX response Text? [duplicate]

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?

like image 730
DNB5brims Avatar asked Aug 04 '09 03:08

DNB5brims


People also ask

How do I return a response text in Ajax?

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.

How do I stop multiple Ajax calls from repeated clicks?

click(function(e) { e. preventDefault(); if ( $(this). data('requestRunning') ) { return; } $(this). data('requestRunning', true); $.

What is response text in Ajax?

The responseText method is used for all formats that are not based on XML. It returns an exact representation of the response as a string. Plain text, (X)HTML, and JSON are all formats that use responseText.

Does Ajax return a promise?

ajax returns, which is a jqXHR object that conforms to the promise interface. If there is a failure, the outer fail function is invoked. The outer fail function is also invoked if the processData function fails. When both the getData and processData functions are successful, the outer done method is invoked.


2 Answers

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); }); 
like image 161
Marius Avatar answered Sep 21 '22 13:09

Marius


How about adding "asynchronous: false" in your code? In my case, it worked well :)

like image 26
someone Avatar answered Sep 23 '22 13:09

someone