Load .json file in jQuery and assigned the file data to a variable

时间:2016-10-20 12:55:53

标签: javascript jquery json

I have a file named data.json I want the data in this file assigned to a variable. I have been searching and found getJSON but not sure how to use it.

With the following code I can see all my data from the data.json file in my chrome's console

$.getJSON('data.json', function(data){
   console.log(data);
})

But I'm trying to do something like this. I know I'm doing it wrong because my code doesn't work but sharing it so you can get the idea.

var myData;

$.getJSON('data.json', function(data){
   myData = data;
});

console.log(myData);

So is there any way to assign the json file's data to a variable?

0 个答案:

没有答案