2013-09-24 60 views
0

我一直在试图弄清楚如何使用AJAX请求更新全局变量。我卡住了,需要一些帮助。使用AJAX请求进行异步数组更新

var markerArray = []; 

function JSONload(position){ //Loads JSON and markers 
console.log("data getting jsoned"); 
$.getJSON('json/***.json', function(result){ //gets the json and parses it into results 
    $.each(result, function(index, value){ //for each result, give the index and the value  
    reps.push({value: value, index: index}); //push the value at to the array. ex. Ian Calderon 
    }); 


    try{ 
    for (var i=0; i<reps.length; i++){ 
     if(reps[i].value.lat !== 'undefined' && reps[i].value.position === position){   
     var marker = (createMarker(reps[i].value.lat, reps[i].value.long, reps[i].index)); //only try to create the marker if there is the values 
     updateArray(markerArray, marker); 

      }}} 
    catch(e){ 
    } 
    }); 

}

我也试着这样做:

function updateArray(array, obj){ 
$.ajax({ 
    success : function(){ 
     array.push(obj); 
    } 
} 
    ); 

但坦率地说我不知道​​如何着手

回答

0

我觉得功能范围扔你。

试试这个:

var that = this; 
console.log("data getting jsoned"); 

    updateArray(that.markerArray, marker); 

的变量应该有访问您的全局变量的函数。