So I’m working on something where I assign a global array’s values from a JSON object, and then use them later. Here’s what I’m looking at
var Arr = new Array();
function parseFile() {
$.ajax({
type: "GET",
url: "openCSVScript.php",
success: function(data){
Arr = jQuery.parseJSON(data);
Console.log(Arr[0]); //shows a proper value
}
});
}
However, in another function this line is used:
console.log("Length of Arr: " + Arr.length); //displays 0
Any ideas of what I’m doing wrong?
You never assign anything to
callNumArr. So it’ll always be empty.Besides that, chances are good that your function is called before the asynchronous request finished. Any code that relies on
callNumArrshould be called from thesuccesscallback unless it’s guaranteed in some other way that it won’t execute before the AJAX request finished (but that’s kind of unlikely, the request could e.g. take longer than expected etc).