ajax请求步骤(4步)

1、创建请求
// IE7+
var xhr
if(window.XMLHttpRequest) {
	xhr = new XMLHttpRequest();
} else {
// IE5 IE6
	xhr = new ActiveXObject("Micrsoft.XMLHTTP");
}
2、连接服务器
// open参数结构:open(method, url, async, user, psw);
// method:GET/POST url:服务器(文件)位置, async:true-异步/false-同步 user:可选用户名, psw:可选用户密码
xhr.open("GET", url, true); 
3、发送请求
xhr.send();  // GET请求
xhr.send(string);  // POST请求,可通过send发送需要的字符串
4、接收返回值
xhr.onreadystatechange = function() {
	if(this.readyState==4 && this.status==200){ //请求已完成响应已就绪且请求返回状态码是200
		responseCallBack(); // 执行自定义的响应回调函数或者操作
	}
}

更多推荐

ajax请求步骤