jquery 异步和 JSON 数据

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

jquery async and JSON data

jqueryjsonasynchronous

提问by Linus

following from javascript jquery and using evali still could not get jquery to read the data asynchronously.

遵循javascript jquery 并使用 eval我仍然无法让 jquery 异步读取数据。

 data1=[1,2,3,4]

Note: i have included async:true in the below example just to show the difference

注意:我在下面的示例中包含了 async:true 只是为了显示差异

Below example return "null"

下面的示例返回“null”

$(document).ready(function(){

var myArray=[];
myArray=getValues();
alert(myArray);
        function getValues(){
        var result=null;
             $.ajax({
                url: 'data1.html',
                type: 'get',
                dataType: 'json',
                cache: false,
                success: function(data) {result = data;},
                async:true,
                });
            return result;
        };
})

and below example work fine and gives the result in an array i.e [1,2,3,4]

和下面的例子工作正常,并在一个数组中给出结果,即 [1,2,3,4]

$(document).ready(function(){

var myArray=[];
myArray=getValues();
alert(myArray);
        function getValues(){
        var result=null;
             $.ajax({
                url: 'data1.html',
                type: 'get',
                dataType: 'json',
                cache: false,
                success: function(data) {result = data;},
                async:false,
                });
            return result;
        };
 })

can someone explain how to get the results asynchronously Thanks

有人可以解释如何异步获取结果吗?谢谢

回答by coder_tim

I would change it to this ...

我会把它改成这个......

$(document).ready(function(){

     function postProcessing(data) {
       var myArray = data;
       alert(myArray);
     }


    getValues();

        function getValues(){
             $.ajax({
                url: 'data1.html',
                type: 'get',
                dataType: 'json',
                cache: false,
                success: postProcessing,
                async:true,
                });
        };
})

回答by Santosh Linkha

This should work, as it has worked in mine, but i suggest you not to do it.

这应该有效,因为它在我的工作中有效,但我建议你不要这样做。

<script src="jquery.js"></script>
<script>
$(document).ready(function(){

    /*don't do your stuff here*/
        /*do inside success*/

    function getValues(){
        var result=null;
        $.ajax({
            url: 'phpinfo.php',
            type: 'get',
            dataType: 'json',
            cache: false,
            success: function(data) { if(data != null){ alert(data); } },
        });
        return result;
    };

})
</script>

回答by yangqi

first part, the result is returned asynchronously to myArray, but before that, the alert function has already executed because the ajax happened asynchronously. So if you place alert right after "return result", you should see the result.

第一部分,结果异步返回给myArray,但在此之前,由于ajax异步发生,alert函数已经执行。因此,如果您在“返回结果”之后立即放置警报,您应该会看到结果。

回答by Payal2299

By default, all requests are sent asynchronously. trueby default.

默认情况下,发送所有请求asynchronouslytrue默认情况下。

If you need synchronous requests, set this option to false. Cross-domainrequests and dataType: "jsonp"requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

如果您需要同步请求,请将此选项设置为falseCross-domainrequests 和dataType: "jsonp"requests 不支持同步操作。请注意,同步请求可能会暂时锁定浏览器,从而在请求处于活动状态时禁用任何操作。

If you need to ensure that the method always displays the latest data then use cache:falseand async:false. Also to avoid timeout/error use success/error/completecallback options.

如果您需要确保该方法始终显示最新数据,请使用cache:falseasync:false。同时,为了避免超时/错误使用success/ error/complete回拨选项。