jquery async and JSON data

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

Below example return "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]

$(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 Спасибо

5
задан Community 23 May 2017 в 11:54
поделиться