Reputation: 13
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?
Upvotes: 0
Views: 2817
Reputation: 6911
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/
Upvotes: 2