手写Ajax

155 阅读1分钟
var xhr = new XMLHttpRequest()   //创建对象
xhr.open('GET','/xxx')     // 设置请求和路径 有第三个参数,默认是异步
xhr.onreadystatechange = function(){
    if(xhr.readyState === 4){
        if(xhr.status >=200 && xhr.status <=300 || xhr.status === 304){
            success(xhr)    //success定义好的回调
        }else{
            fail(xhr)
        }
    }
}

xhr.send('{"name":"zou"}')