天天看點

自己封裝簡易版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);
//     }
// })
           

繼續閱讀