I'm trying to access a global array inside the $.getJSON callback. For some reason I can access the array for actually calling the ajax request, but it is undefined within the callback.
JS
var toprint = [
{name: "Name",
year: "2014",
call: "http://api.call.com"},
// etc.....
]
for(var i = 0; i < toprint.length; i++){
$.getJSON( toprint[i].call, function( data) { // toprint[i].call accessed fine
// some code...
// toprint[i] undefined below
jsonPrint = "<div class='col'><h3>" + toprint[i].name + "</h3>" + toprint[i].year + "<ul>";
// some code...
});
}
I am getting the error:
TypeError: toprint[i] is undefined
Which is weird, because according to other SO questions I've read I'm supposed to be able to access global variables inside AJAX callbacks.