I'm trying to load a local JSON file but it won't work. Here is my JavaScript code (using jQuery:
var json = $.getJSON("test.json");
var data = eval("(" +json.responseText + ")");
document.write(data["a"]);
The test.json file:
{"a" : "b", "c" : "d"}
Nothing is displayed and Firebug tells me that data is undefined. In Firebug I can see json.responseText
and it is good and valid, but it's strange when I copy the line:
var data = eval("(" +json.responseText + ")");
in Firebug's console, it works and I can access data.
Anyone have a solution?
Found this thread when trying (unsuccessfully) to load a local json file. This solution worked for me...
... and is used like this...
...and this is the
<head>
...$.getJSON
is asynchronous so you should do: