原生ajax写法

691 阅读1分钟


function ajax({url='',type='get',dataType:'json'}){

     return  new Promise((resolve,reject)=>{

            let xhr = new XMLHttpRequest();

            xhr.open(type,url,true);

            xhr.responseType = dataType;

            xhr.onload = function(){

                  resolve(xhr,response)

            }

            xhr.onerror = function(err){

                 reject(err)

            }

            xhr.send();

    })

}

ajax({url:'./a.json'}).then(function(data){console.log(data)},function(data){console.log(data0)})