123456789101112131415161718192021222324252627282930313233 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- // 前后端交互一般传输的是字符串 JSON字符串
-
- // 第一步创建XMLHttpRequest对象;
- let xhr = new XMLHttpRequest();
- // 第二步 调用open方法 配置请求信息 两个参数第一个请求方式 第二个请求地址(请求接口)
- xhr.open("GET","http://shop-api.edu.koobietech.com/prod/tagProdList");
- // 第三步 发送请求
- xhr.send();
- // 第四步 监听响应
- xhr.onreadystatechange = function(){
- if(xhr.readyState == 4 && xhr.status == 200){
- // xhr.responseText 服务端返回的数据
- let jsonStr = xhr.responseText;
- // 服务端返回的数据为JSON字符串
- // 把JSON字符串转换为对象
- let obj = JSON.parse(jsonStr);
- console.log(obj);
- }
- }
- </script>
- </body>
- </html>
|