How to load my local json file using JavaScript

My json file is as follows

{ "Persons": { "Name" : "e", "Name2": "e", "Id": "4700" }, [...] } 

What does my code look for parsing / loading this local json file into an html file. I tried everything, but none of them worked.

+7
javascript ajax serialization
source share
1 answer

Here is an example from ( http://youmightnotneedjquery.com/ )

 request = new XMLHttpRequest(); request.open('GET', '/my/url', true); request.onload = function() { if (request.status >= 200 && request.status < 400){ // Success! data = JSON.parse(request.responseText); } else { // We reached our target server, but it returned an error } }; request.onerror = function() { // There was a connection error of some sort }; request.send(); 

Your data variable will have accessible elements like this:

alert(data.Persons.Name);

+6
source share

All Articles