ajax/post请求

 1 <script>
 2     window.addEventListener('load', function (ev) {
 3         var btn = document.getElementById('send');
 4         btn.addEventListener('click', function (ev1) {
 5 
 6             // 1. 获取用户输入的内容
 7             var account = document.getElementById('account').value;
 8             var pwd = document.getElementById('pwd').value;
 9 
10             // 2. 创建网络请求对象
11             var xhr = new XMLHttpRequest();
12             xhr.open('post', 'http://localhost:3000/api/five', true);
13             // 设置请求头
14             xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
15 
16             xhr.send('name=zs&age=19');
17             xhr.addEventListener('readystatechange', function (ev2) {
18                 if(xhr.readyState === 4 ){
19                    if(xhr.status === 200){
20                        console.log(xhr.responseText);
21                    }else {
22                        console.log('请求失败!');
23                    }
24                 }
25             });
26         });
27     });
28 </script>

猜你喜欢

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