// 先实例化
const xhr = new XMLHttpRequest();
// 监听响应
xhr.onreadystatechange = () => {
// 我们只关心4
if (xhr.readyState !== 4) {
// 不往下走
return false;
}
// 如果状态是4, 判断状态码
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
// 说明可以获取数据
// 打印数据
console.log("正常获取数据");
console.log(xhr.responseText);
}
};
// 定义open
xhr.open("get", "/demo.json", true);
// 定义send, 需要发送的数据
xhr.send(null);