let ajax = {
get(url,fn){
//创建XMLHttpRequest对象
let xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
//与服务器建立连接
xhr.open('get',url,true);
//发送请求
xhr.send();
//等待响应
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status === 200){
if(typeof fn === 'function'){
fn(xhr.responseText);
}
}
}
}
},
post(url,data,fn){
let xhr = window.XMLDocument ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('post',url,true);
xhr.setRequestHeader('Content-type','Application/x-www-form-urlencoded;charset=utf-8');
xhr.send(data);
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status === 200){
if(typeof fn === 'function'){
fn(xhr.responseText);
}
}
}
}
}
}
ajax的封装
最新推荐文章于 2024-08-12 17:51:29 发布