如何使用JavaScript加载我的本地json文件

145*_*453 7 javascript ajax serialization

我的json文件看起来像这样

{
    "Persons": {
        "Name" : "e",
        "Name2": "e",
        "Id": "4700"
    }, [...]
}
Run Code Online (Sandbox Code Playgroud)

我的代码如何将此本地json文件解析/加载到html文件中.我尝试了一切,但没有一个工作.

Sea*_*dle 6

这是(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();
Run Code Online (Sandbox Code Playgroud)

您的data变量将具有如下可访问的成员:

alert(data.Persons.Name);