jquery异步和JSON数据

Lin*_*nus 5 jquery json asynchronous

javascript jquery和使用eval我仍然无法获得jquery异步读取数据.

 data1=[1,2,3,4]
Run Code Online (Sandbox Code Playgroud)

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

下面的例子返回"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;
        };
})
Run Code Online (Sandbox Code Playgroud)

以下示例工作正常,并以数组形式给出结果,即[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;
        };
 })
Run Code Online (Sandbox Code Playgroud)

有人可以解释如何异步获得结果谢谢

cod*_*tim 8

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

$(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,
                });
        };
})
Run Code Online (Sandbox Code Playgroud)