长话短说,我正在尝试将返回 AJAX 的 JSON 中的相应数据值存储到这些全局数组中。我知道数组构造正确,因为我已经在 AJAX 中放置了警报,但是当我将它放在 AJAX 之外时,数组仍然是未定义的。
如何导出整个 popData JSON 对象以对其进行处理并将值存储在全局数组中,或者在 AJAX 中填充数组以在调用之外进行?我需要另一个函数可以访问这些数组,以将总体值与用户选择的窄范围值进行比较——如果有人想提出更好的方法,但它必须在 onLoad 上提取总体值已经在 HTML 中完成。我认为这是在服务器上使用最少的 AJAX 调用来做到这一点的最简化的方法,但我愿意接受建议!:D
var popProducers = new Array();
var popProducersCount = new Array();
function getPopulationInfo(){
$.ajax({
url:phpURL,
cache:false,
dataType:"json",
data: { },
success:function(popData){
for (i=0;i<popData.length;i++){
//producers and producersCount should be the same length at all times!
//If current producer name isn't in array already
if(popProducers.indexOf(popData[i].ProducerName) == -1){
//add new element to represent new producer quantity (producerCount.length returns number of elements in the array, thus if there are no elements = 0 thus making index 0 equal to 1 and so on)
popProducersCount[popProducersCount.length] = 1;
//Adds the producer name to the list of producers
popProducers[popProducers.length] = popData[i].ProducerName;
} else {
//Otherwise, it will increment the index of the producersCount array corresponding with the pre-existing producer name's index by 1
popProducersCount[popProducers.indexOf(popData[i].ProducerName)] += 1;
}
}
}
});
alert("Population Data Alert: " + popProducers);