23_ajax.html 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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. // 前后端交互一般传输的是字符串 JSON字符串
  11. // 第一步创建XMLHttpRequest对象;
  12. let xhr = new XMLHttpRequest();
  13. // 第二步 调用open方法 配置请求信息 两个参数第一个请求方式 第二个请求地址(请求接口)
  14. xhr.open("GET","http://shop-api.edu.koobietech.com/prod/tagProdList");
  15. // 第三步 发送请求
  16. xhr.send();
  17. // 第四步 监听响应
  18. xhr.onreadystatechange = function(){
  19. if(xhr.readyState == 4 && xhr.status == 200){
  20. // xhr.responseText 服务端返回的数据
  21. let jsonStr = xhr.responseText;
  22. // 服务端返回的数据为JSON字符串
  23. // 把JSON字符串转换为对象
  24. let obj = JSON.parse(jsonStr);
  25. console.log(obj);
  26. }
  27. }
  28. </script>
  29. </body>
  30. </html>