Im trying to load the content of a JSON File into an variable. Before, the variable looked something like this:
var Data = {
teams : [
["Team 1", "Team 2"],
["Team 3", "Team 4"]
],
results : [
[[1,2], [3,4]],
[[4,6], [2,1]]
]}
Now I have a JSON File looking something like this:
{"teams":[["Team 1","Team 2"],["Team 3","Team 4"],"results":[[[[1,2],[3,4]],[[4,6],[2,1]]]}
Now I want that the the content of the JSON File is stored in the Data Variable before. I tried it with Ajax which looks like this:
$.ajax({
type: "GET",
dataType : 'json',
async: true,
url: 'data.json',
success: function(data) {
console.log(data)
var Data = data
},
});
Console.log works perfect, but the Data is not saved in the variable and I'm getting the error: Uncaught ReferenceError: Data is not defined.
I also tried it with var Data = JSON.parse(data), but this doesn't seem to work either.
And now I'm stuck without any clue.
Thanks for help in advance.
var Datawill only be available within the scope of thesuccesscallback. You'll need a way to store your state where it's available outside that function.Uncaught ReferenceError: Data is not definedmeans that the JS Interpreter found a reference in the name ofDatathat is not defined in the Scoop within which it's being used. SHORT STORY: At some other place, you useDataoutside of async ajax call. You should put that code into success function, or put it into a function with data argument and call that function inside sucess function while passingDataas parameter