25_ajax.html 999 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. function ajaxFun(url,foo) {
  11. // 第一步 创建XMLHttpRequest 对象;
  12. var xmlh = new XMLHttpRequest();
  13. // 第二步 发送请求
  14. xmlh.open("GET", url, true);
  15. xmlh.send();
  16. // 第三步 处理请求结果
  17. xmlh.onreadystatechange = function () {
  18. if (xmlh.readyState == 4 && xmlh.status == 200) {
  19. var resObj = JSON.parse(xmlh.responseText)
  20. // console.log(resObj)
  21. // return resObj;
  22. foo(resObj)
  23. }
  24. }
  25. }
  26. // console.log(ajaxFun("./data/data1.json"));
  27. ajaxFun("./data/data1.json",function(res){
  28. console.log(res);
  29. })
  30. </script>
  31. </body>
  32. </html>