ajax/get请求

 1 <script>
 2    window.addEventListener('load', function (ev) {
 3        var btn = document.getElementById('send');
 4        btn.addEventListener('click', function (ev1) {
 5             // 1. 创建一个请求对象
 6             var xhr = new XMLHttpRequest();
 7 
 8             // 2. 准备发送
 9             xhr.open('get', 'http://localhost:3000/api/one', true);
10 
11             // 3. 发送请求
12             xhr.send();
13 
14             // 4. 监听服务器的响应
15             xhr.addEventListener('readystatechange', function (ev2) {
16                  // console.log(xhr.readyState);
17                  if(xhr.readyState === 4){
18                      // 意味着服务器的响应结束了
19                      // 不代表副武器的响应是一定正确
20                      // console.log(xhr.status);
21                      if(xhr.status === 200){
22                          console.log(xhr.response);
23                          console.log(xhr.responseText);
24                      }
25                  }
26             });
27        });
28    });
29 </script>

猜你喜欢

转载自www.cnblogs.com/zhangzhengyang/p/11223430.html