升级你的 jQuery.ajax 满足 Promise 规则 jQuery.ajax({ url: '/xxx', method: 'get' }).then(success,...

window.jQuery = function(){}
window.jQuery.ajax = function({url,method,body,headers}){
    return new Promise(function(resolve,reject){
        let request = new XMLHttpRequest()
        request.open(method,url)
        for(let key in headers){
            let value = headers[key]
            request.setRequestHeader(key,value)
            request.onreadystatechange = ()=>{
                if(request.readyState === 4){
                    if(request.status >=200 && request.status < 300){
                        resolve.call(undefined,request.responseText)
                    }else if(request.status > 400){
                        reject.call(undefined,request)
                    }
                }

            }
            request.send(body)
        }
    })
}
button.addEventListener('click',function(){
jQuery.ajax({
    url:'./xxx',
    method:'post',
    body:'123',
    headers:{
        'frank':'18'
    }
}).then((x)=>{console.log(x)},
    (x)=>{console.log(x)}
)
})

你可能感兴趣的:(升级你的 jQuery.ajax 满足 Promise 规则 jQuery.ajax({ url: '/xxx', method: 'get' }).then(success,...)