Skip to content
Fix Code Error

Loading local JSON file

March 13, 2021 by Code Error
Posted By: Anonymous

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.

Does anyone have a solution?

Solution

$.getJSON is asynchronous so you should do:

$.getJSON("test.json", function(json) {
    console.log(json); // this will show the info it in firebug console
});
Answered By: Anonymous

Related Articles

  • How to properly do JSON API GET requests and assign output…
  • How to parse JSON with XE2 dbxJSON
  • The 'compilation' argument must be an instance of…
  • Azure Availability Zone ARM Config
  • Avoid creating new session on each axios request laravel
  • Search match multiple values in single field in…
  • Event Snippet for Google only shows one event while testing…
  • Why does this Azure Resource Manager Template fail…
  • NullpointerException error while working with choiceBox and…
  • loop and eliminate unwanted lines with beautiful soup

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do you select a particular option in a SELECT element in jQuery?

Next Post:

Download single files from GitHub

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error