0

I need to load a JSON file in my app in JavaScript.

The json file is like this:

{
  "list": [
    {
      "item1": "text",
      "item2": "text",
      "item3": "text",
      "item4": "text"
    },
    {
      "item1": "text",
      "item2": "text",
      "item3": "text",
      "item4": "text"
    },
    ...
  ]
}

I've searched, but I always find solutions using AJAX and PHP, but I don't (and I can't) use any PHP server. So I've looked from the side of jQuery and the getJson() function, but that's didn't work, the JavaScript JSON.parse() didn't work for my file.

So, do any of you know how to load a local file with JavaScript or any other API?

1
  • Can you post the code that didn't work and any error messages? $.getJson() should already parse the JSON, no need to call JSON.parse(). Commented Apr 12, 2014 at 23:50

1 Answer 1

2

You can't open a local file using javascript. The browser will not allow you, this is a security issue. (Save for using HTML5 FileAPI, but I don't think that is what you are asking)

However, you can place your .json files on a web server and load via a URL them using jquery.

$.getJSON("http://yourdomain.com/somefolder/test.json", function( data ) {
  //do something here with json data
});

http://api.jquery.com/jquery.getjson/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.