Ajax基本交互步骤

1.创建一个ajax对象

var xhr = null;           

IE6以下:xhr = new ActiveXObject(‘Microsoft.XMLHTTP’);
其余浏览器:xhr = new XMLHttpRequest();

2.调用open方法 设定获取数据的地址

open方法参数
1.打开方式(get或post)
2.地址
3.是否异步
异步:非阻塞 前面的代码不会影响后面代码的执行
同步:阻塞 前面的代码会影响后面代码的执行
xhr.open(‘get’,‘1.txt’,true);

3.提交 向服务器发送请求

xhr.send();

4.等待服务器返回内容

xhr.onreadystatechange = function() {
	if ( xhr.readyState == 4 ) {
		if ( xhr.status == 200 ) {
			alert( xhr.responseText );
		} else {
			alert('出错了,Err:' + xhr.status);
		}
}

readyState : ajax工作状态

responseText : ajax请求返回的内容就被存放到这个属性下面

on readystate change : 当readyState改变的时候触发

status : 服务器状态,http状态码

猜你喜欢

转载自blog.csdn.net/yijun9588/article/details/89093197