0

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();
verlager
  • 794
  • 5
  • 25
  • 43
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Robby Cornelissen May 23 '22 at 01:21
  • AJAX is **asynchronous**... – Robby Cornelissen May 23 '22 at 01:22
  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Phil May 23 '22 at 01:47

1 Answers1

0

I simply use php.

members = <?php include('assets/uscf/uscf.json');?>;
verlager
  • 794
  • 5
  • 25
  • 43