<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<h2>使用 XMLHttpRequest 来获取文件内容</h2>
<p>文件内容是标准的 JSON 格式,可以使用 JSON.parse 方法将其转换为 JavaScript 对象。</p>

<p ></p>

<script>

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
        document.getElementById("demo").innerHTML = myObj.name;
    }
};
xmlhttp.open("GET", "/try/ajax/json_demo.txt", true);
xmlhttp.send();

</script>

<p>查看 JSON 文件数据 <a href="/try/ajax/json_demo.txt" target="_blank">json_demo.txt</a></p>

</body>
</html>

  

相关文章:

  • 2021-12-12
  • 2022-12-23
  • 2018-01-11
  • 2021-08-02
  • 2021-10-02
  • 2021-05-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-15
  • 2021-12-04
  • 2021-11-15
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案