javascript jQuery AJAX:成功时返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9055810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:26:56  来源:igfitidea点击:

jQuery AJAX: return value on success

javascriptjqueryajax

提问by Ticksy

        function ChatServerQuery(data_json) {

        var result = null;

        $.ajax({
            url: 'chat/backend/',
            type: 'POST',
            data: data_json,
            success: function(json) {
                result = json
            }
        })

        return result

    }

My function that executes a request to the server. The problem is that I can not return received from the server text. I do not know how to take from an anonymous function (event success) to ChatServerQuery (where you can easily get it back).

我的函数执行对服务器的请求。问题是我无法返回从服务器收到的文本。我不知道如何从匿名函数(事件成功)到 ChatServerQuery(您可以轻松地将其取回)。

回答by dfsq

You'd better change your approach to reflect an asynchronous nature of AJAX request.

您最好更改您的方法以反映 AJAX 请求的异步性质。

Using callback function

使用回调函数

function ChatServerQuery(data, callback) {
    $.ajax({
        url:  'chat/backend/',
        type: 'POST',
        data: data,
        success: callback
    });
}

Then you would use it:

然后你会使用它:

ChatServerQuery(dataObject, function(data) {
    // work with your data came from server
});

Using promise object

使用承诺对象

$.fn.ajaxreturns object implementing Promiseiterface, so you can use it like this:

$.fn.ajax返回实现Promiseiterface 的对象,因此您可以像这样使用它:

function ChatServerQuery(data) {
    return $.ajax({
        url:  'chat/backend/',
        type: 'POST',
        data: data
    });
}

ChatServerQuery(dataObject).done(function(data) {
    // work with your data came from server
});

This option offers you more flexibility.

此选项为您提供了更大的灵活性。

回答by James McLaughlin

I answered something similar earlier:

我之前回答过类似的问题:

Your function returns resultlong before the AJAX callback executes. Anything that depends on the result of the request hasto happen in or after the callback.

您的函数result在 AJAX 回调执行之前很久就返回了。任何依赖于请求结果的事情必须在回调中或之后发生。

回答by clime

Instead of trying to assign jsonto resultin the success handler, do your logic there (or in functions called from it). Or if you need to wait for the response, follow the other comments.

不要尝试在成功处理程序中分配jsonresult,而是在那里(或从它调用的函数中)执行您的逻辑。或者,如果您需要等待回复,请按照其他评论进行操作。

回答by Krishna

function ChatServerQuery(url,data) {

功能 ChatServerQuery(网址,数据){

return $.ajax({ type: "POST",
    url: url,
    data: data,
    async:false,
    success: function(status){
    }
}).responseText;

}

}

//Now you will get the response text from server side via ajax call

//现在您将通过ajax调用从服务器端获取响应文本

回答by Ricardo Souza

The request needs to be synchronous to work in this way.

请求需要同步才能以这种方式工作。

Add async: falseto the parameters object of the ajax()method.

添加async: false到方法的参数对象ajax()