let ajax=(obj)=>{
return new Promise((resolve,reject)=>{
let method=obj.method||'GET';
let xhr=null;
if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}else{
xhr=new ActiveObject('Microsoft.XMLHTTP);
}
xhr.onreadystatechange=()=>{
if(xhr.readyState==4&&xhr.status==200){
reslove(xhr.responseText);
}else{
reject(xhr.status);
}
}
if(method=='POST'){
xhr.open('POST',obj.url,true);
}
else{
let query = '';
for (let key in obj.data) {
query += '&' + encodeURIComponent(key) + "=" + encodeURIComponent(obj.data[key]);
}
query.substring(1);
xhr.open('GET', obj.url + '?' + query, true);
xhr.send();
}
xhr.timeout=1000;//设置超时时间
});
}
Promise封装一个ajax
猜你喜欢
转载自blog.csdn.net/qq_23864401/article/details/88236357
今日推荐
周排行