How to return AJAX response Text?

This question already has an answer here:

  • How do I return the response from an asynchronous call? 33 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);
    });
    

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

    链接地址: http://www.djcxy.com/p/9488.html

    上一篇: jQuery ajax返回值

    下一篇: 如何返回AJAX响应文本?