Ajax jquery 异步返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4555079/
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-08-26 17:28:09  来源:igfitidea点击:

Ajax jquery async return value

jqueryajaxasynchronousreturn

提问by Ghandhikus

how can i make this code to return the value without freezing the browser.
You can rewrite this with new method of course.

我怎样才能让这段代码在不冻结浏览器的情况下返回值。
你当然可以用新方法重写它。

function get_char_val(merk)
{  
    var returnValue = null;
    $.ajax({   
                type:       "POST",
                async:      false,   
                url:            "char_info2.php",   
                data:   { name: merk },   
                dataType: "html",  
                success:    function(data)
                                    {
                                        returnValue = data;
                                    } 
        }); 
    return returnValue;
}
var px= get_char_val('x');
var py= get_char_val('y');

EDIT:i need to have at least 20 variables get from php file at other times.

编辑:我需要在其他时间从 php 文件中获取至少 20 个变量。

回答by SLaks

This is not possible.
Javascript runs on the UI thread; if your code waits for the server to reply, the browser must remain frozen.

这不可能。
Javascript 在 UI 线程上运行;如果您的代码等待服务器回复,则浏览器必须保持冻结状态。

Instead, you need to return the value using a callback:

相反,您需要使用回调返回值:

function get_char_val(merk, callback)
{  
    var returnValue = null;
    $.ajax({   
                type:       "POST",
                url:            "char_info2.php",   
                data:   { name: merk },   
                dataType: "html",  
                success:    function(data) {
                    callback(data);
                } 
        }); 
}

get_char_val('x', function(px) { ... });
get_char_val('y', function(py) { ... });

Note that the two callbacks will run in an unpredictable order.

请注意,这两个回调将以不可预测的顺序运行。



You should modify your design so that you can get all twenty values in a single AJAX request.
For example, you can take a comma-separated list of values, and return a JSON object like { x: "...", y: "..." }.

您应该修改您的设计,以便您可以在单个 AJAX 请求中获得所有二十个值。
例如,您可以采用逗号分隔的值列表,并返回一个 JSON 对象,如{ x: "...", y: "..." }.

回答by Dutchie432

You can not make variable assignments this way (async). you must set the variables in the successhandler.

您不能以这种方式(异步)进行变量分配。您必须在success处理程序中设置变量。

variableArray = new Array(); 

get_char_val('x');
get_char_val('y');

function get_char_val(merk)
{  
    var returnValue = null;
    $.ajax({   
        type:       "POST",
        url:            "char_info2.php",   
        data:   { name: merk },   
        dataType: "html",  
        success:    function(data)
            {
                variableArray[merk] = data;
            } 
    }); 
}

Once all of the retrievals are finished you can access the x and y variables by using variableArray[x]and variableArray[y]

完成所有检索后,您可以使用variableArray[x]和访问 x 和 y 变量variableArray[y]

回答by Suhumar

This may not be what you are expecting but you can set async: true if you dont want pause in the browser and so what you want to do with px on success

这可能不是你所期望的,但你可以设置 async: true 如果你不想在浏览器中暂停,那么你想在成功时用 px 做什么