XMLHttpRequest : 请求服务器上的数据资源
如何 用 xhr 向服器发送请求
//1创建
var xhr = new XMLHttpRequest();
//2、调用 open 函数
xhr.open('GET', 'http://www.liulongbin.top:3006/api/getbooks');
// 3、调用send函数
xhr.send();
// 4、监听 onreadystatechange事件
xhr.onreadystatechange = function() {
// readyState xhr的请求状态 status 是xhr的服务器响应状态
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText);
}
};
// readyState xhr的请求状态
// 0 创建未调用
// 1 open() 方法已调用
// 2 send() 方法已调用 响应该已被 接收
// 3 数据 接收中
// 4 done ajax请求完成 或失败
$.