首页 > Web开发 > 详细

ajax/post请求

时间:2019-07-22 11:10:32      阅读:65      评论:0      收藏:0      [点我收藏+]
 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>

 

ajax/post请求

原文:https://www.cnblogs.com/zhangzhengyang/p/11223441.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!