代码示例:(标识:eg_js_json_php_db)
<!DOCTYPE html>
<html>
<body>

<h1>通过服务器上的 PHP 获取 JSON 数据</h1>

<p>从 PHP 文件接收的 JSON:</p>

<p id="demo"></p>

<script>
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    document.getElementById("demo").innerHTML = this.responseText;
  }
};
xmlhttp.open("GET", "/static/demo/php/demo_json_db.php", true);
xmlhttp.send();
</script>

</body>
</html>
运行结果: