var xhr;
if(window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}else {
xhr = ActiveXObject("Micsoft.XMLHTTP");//兼容老版本Ie
}
//xhr.open();参1,get/post请求;参2:url;参3:true/false;异步/同步,可省略,默认异步
xhr.open(‘get',url ,);//用于get请求
xhr.send();
//xhr.open('post',url);
//xhr.send('name=xxx&age=11');//post请求发送的参数
xhr.onreadystatechange = function() {
if(xhr.readyState==4){
//0: 请求未初始化
//1: 服务器连接已建立
//2: 请求已接收
//3: 请求处理中
//4: 请求已完成,且响应已就绪
if(xhr.status>=200&&xhr.status<300){
console.log(xhr.responseText);//返回的数据
//其他操作
}
}
}