**
简单的原生Ajax请求步骤
**
//1.创建xhr对象并判断兼容性
var xhr = null;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest( ); //IE 6以上
} else {
xhr = new ActiveXObject(“Microsoft.XMLHTTP”); //IE6 以下
}
//2.准备发送请求
xhr.open(method,url, async);
//第一个参数是请求方式;
//第二个参数是请求地址;
//第三个参数是同步还是异步 异步是true 同步是false
//如果是GET请求 send()可以为空,如果是POST请求send()必须是字符串string
//如果是POST请求 在open和send中间还需要添加请求头,不然会报错
xhr.setRequestHeader(“Content-type”,“application/x-www-form-urlencoded” );
//3.发送请求
xhr.send( );
//4.执行回调函数
xhr.onreadystatechange = function () {
//判断服务器是否正确响应
if (xhr.readystate = 4 && xhr.status = 200){
console.log(xhr.responseText);
}
}