如何返回AJAX响应文本?(复制)

时间:2022-10-09 07:37:22

This question already has an answer here:

这个问题已经有了答案:

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

我使用prototype进行AJAX开发,我使用如下代码:

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?

但它也不起作用。如何获得要使用的其他方法的responseText ?

2 个解决方案

#1


28  

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):

记住,onComplete在someFunction完成工作后很长时间才调用。您需要做的是将回调函数作为参数传递给somefunction。当过程完成(即完成)时,将调用此函数:

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);
});

#2


3  

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

在代码中添加“异步:false”如何?就我而言,它很管用:

#1


28  

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):

记住,onComplete在someFunction完成工作后很长时间才调用。您需要做的是将回调函数作为参数传递给somefunction。当过程完成(即完成)时,将调用此函数:

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);
});

#2


3  

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

在代码中添加“异步:false”如何?就我而言,它很管用: