遍历来自数据库的结果

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>通过服务器上的 PHP 文件获取 JSON 数据</h1>
  6.  
  7. <p id="demo"></p>
  8.  
  9. <script>
  10. var xmlhttp, myObj, x, txt = "";
  11. xmlhttp = new XMLHttpRequest();
  12. xmlhttp.onreadystatechange = function() {
  13. if (this.readyState == 4 && this.status == 200) {
  14. myObj = JSON.parse(this.responseText);
  15. for (x in myObj) {
  16. txt += myObj[x].CustomerId + "<br>";
  17. }
  18. document.getElementById("demo").innerHTML = txt;
  19. }
  20. };
  21. xmlhttp.open("GET", "/demo/demo_json_db.php", true);
  22. xmlhttp.send();
  23. </script>
  24. </body>
  25. </html>