使用Promise封装ajax

const promiseAjax = (url,data)=>{
    return new Promise((resolve,reject)=>{
        const xhr = new XMLHttpRequest();
        xhr.open(POST,url);
        xhr.setRequestHeader('ContentType','application/json')
        xhr.send(data)
        xhr.onreadystatechange = function(){
            if(xhr.readyState==4){
                if(xhr.status>=200&&xhr.status<400){
                    resolve(xhr.response)
                }else{
                    reject(xhr.status)
                }
            }
        }
    })
}
posted @ 2022-03-03 18:53  BONiii  阅读(158)  评论(0编辑  收藏  举报