I have a JSON file that looks something like this:
var sourceData = [
{
"id":"c1",
"title":"What color are your eyes?"
}
]
I want to load it via jQuery so I can retrieve the data later:
var myData = $.getScript('data_c1.json', function(data, textStatus){
console.log(data);
return data
});
console.log(myData)
When I look in the console, 2 things are not what I expect:
- the logs hit the console in reverse order
data
returns correctly, howevermyData
returns the$.getScript
function itself, rather than the data.
For the record, I also tried
$.getJSON("sourceData_c1.json", function(json) {console.log(json);});
and that didn't work for me at all.
I'm stumped. Any help greatly appreciated!
Also, is it valid syntax to place var sourceData =
inside the JSON file?