// 1.创建一个 XMLHttpRequest 类型的对象
var xhr = null;
// 兼容写法
if (window.XMLHttpRequest) {
// 标准浏览器
xhr = new XMLHttpRequest();
} else {
// IE 6 浏览器
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
// 2.open() 方法开启请求
// xhr.open("GET","https://jsonplaceholder.typicode.com/users?id=1");
xhr.open("POST","https://jsonplaceholder.typicode.com/users");
// 设置请求头
// 一般 get 方法不需要设置,而 post 方法必须设置
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
// 3.send() 方法发送一次请求
// 如果是 get 方法请求,不需要再 send 中传参数,它如果想传参数,直接写在网址上(?id=1)
// xhr.send(null);
xhr.send("name=harry&age=19");
// 4.指定回调函数,处理得到的数据
xhr.onreadystatechange = function () {
// 通过判断 xhr 的 readyState ,确定此次请求是否完成
if (this.readyState === 2) {
console.log("headers received",xhr.readyState);
} else if (this.readyState === 3) {
console.log("loading",xhr.readyState)
console.log(xhr.responseText)
} else if (this.readyState === 4) {
console.log("done",xhr.readyState)
console.log(xhr.responseText)
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/276c5cf67ade46a9bf31ee8b46d0099a.png)