Ajax简单步骤

122 阅读1分钟
// 创建xhr对象
let xhr = new XMLHttpRequest();
// 监听状态
xhr.onreadystatechange = function() {
    // 判断状态码
    if (xhr.readyState === 4) {
        // 判断状态码
        if (xhr.status === 200 || xhr.status === 304) {
            // 请求成功
            console.log(JSON.parse(xhr.responseText));
        }
    }
}
// 打开请求
xhr.open('GET', '/example/data/demo.json', true);
// 发送数据
// xhr.send('?color=red')
xhr.send(null)