我一直试图弄清楚如何使用 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);
}
}
);
但坦率地说,我不确定如何进行