天天看点

自己封装简易版ajax

//自己封装 简易版 ajax
function ajax(opt){
    // 1.创建请求对象
    let xml = new XMLHttpRequest();
    //2.创建请求信息
    xml.open(opt.type,opt.url,opt.async);
    //3.发送请求
    xml.send();
    //4.监听响应完成
    xml.onreadystatechange = function(){
        //xml.responseText 接受 的是res.end() 传过来的内容
        opt.success && opt.success(xml.responseText)
    }
}
//调用
// ajax({
//     type:"GET",
//     url:"",
//     async:true,
//     success:function(data){
//         console.log(data);
//     }
// })
           

继续阅读