'array of objects inaccessible outside of $.ajax function
AJAX function that reads json data. Works well.
function getUSCF() {
$.ajax({
type: "Get",
url: "assets/uscf/uscf.json",
dataType: "json",
success: function(data) {
members = [];
temp = JSON.stringify(data);
members = JSON.parse(temp);
alert (JSON.stringify(members));
//members = _.sortBy(members, o => o.Name)
//members = _.uniqBy(members,'Name');
},
error: function(){
alert("json not found");
}
});
}
But the problem is that the created "members" array of objects is not readable outside of the function getUSCF().
function initJSON() {
getUSCF(); alert(JSON.stringify(members)); // Uncaught ReferenceError: members is not defined
alert(members.length); // Uncaught ReferenceError: members is not defined
}
initJSON();
Solution 1:[1]
I simply use php.
members = <?php include('assets/uscf/uscf.json');?>;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | verlager |
