jQuery的异步和JSON数据 [英] jquery async and JSON data

查看:109
本文介绍了jQuery的异步和JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery和使用eval 我仍然无法让jQuery来阅读以下异步的数据。

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

 data1=[1,2,3,4]

请注意:我已经包括异步:真正的在下面的例子只是以示区别

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

下面的例子返回空

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

和下面的例子做工精细,并给出结果在数组即[1,2,3,4]

and below example work fine and gives the result in an array i.e [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

推荐答案

我就改成这样...

$(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,
                });
        };
})

这篇关于jQuery的异步和JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆