首页 > 其他 > 详细

6.30

时间:2020-06-30 13:28:25      阅读:43      评论:0      收藏:0      [点我收藏+]

今日学习内容:

# ajax 的四个步骤

1. 创建 XMLHttpRequest 这个对象,这个步骤中需要注意兼容处理
var xhr = null;
if (window.XMLHttpRequest) {
      xhr = new XMLHttpRequest();
} else {
// IE6浏览器
      xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
2. 准备发送(请求的方式,请求的地址,同步还是异步:true异步、false同步)
// xhr.open("get", "checkUsername.php?username=" + username, true);
xhr.open("post", "checkUsername.php", true);
3. 执行发送
// xhr.send(null);
var param = "username=" + username;
// 对于 post 请求来说的话,我们的参数应该放在请求体中
// 设置 xhr 的请求头信息,这个步骤仅仅是针对于 post 请求才有的
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(param);
 4.  设置回调函数
// 4.  设置回调函数
xhr.onreadystatechange = function () {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            // 得到数据
            // xhr.responseXML
            var result = xhr.responseText;
            console.log(result);
            document.getElementById("result").innerText = result;
        }
    }
};

 


    
 
 
 
 

6.30

原文:https://www.cnblogs.com/cntian/p/13213046.html

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