Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i load my local json file just using JavaScript

My json file looks like this

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

How does my code looks like to parse/load this local json file into a html file. I tried everything out but none of them worked.

like image 215
m1711 Avatar asked Dec 08 '14 14:12

m1711


1 Answers

Here's 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 then have accessible members like this:

alert(data.Persons.Name);

like image 188
Sean Kendle Avatar answered Nov 11 '22 12:11

Sean Kendle