手写ajax
2021-03-04
function ajaxPromise(params) { return new Promise((resolve,reject)=>{ let xhr = new XMLHttpRequest() xhr.open(params.type || 'get',params.url,true) xhr.onreadystatechange = function () { if(xhr.readyState === 4&&xhr.status === 200){ resolve(JSON.parse(xhr.responseText)) } } xhr.send(params.data || null) }) } ajaxPromise({ url:'http://route.showapi.com/341-2', data:{ page:'1' } }).then(res=>{ console.log('res',res) })
--------end--------