`

Ajax笔记

    博客分类:
  • Ajax
阅读更多

 

XMLHttpRequest的readyState:

0:未初始化——创建

1:初始化——open

2:发送请求——send

3:开始接受结果

4:接收结果完毕

状态每改变一次,调用一次回调函数。方法调用5次,但是前两次的readyState==1

 

onreadystatechange = callback

绑定回调函数,不加()。

 

XMLHttpRequest的status:

200 OK

404 Not Found

500 Serevr Error

 

 

var xmlHttpRequest;
	
	//创建XMLHttpRequest对象
	function createXmlHttpRequest() {
		var xmlHttp;
		if(window.ActiveXObject) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
			} catch (e) {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
		}else if(window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest()
		}
		
		return xmlHttp;
	}
	

	function checkExist() {
		xmlHttpRequest = createXmlHttpRequest();
		xmlHttpRequest.onreadystatechange = callBack;
		
		var url = "http://localhost:8080/Ajax/CheckUser?uname="+document.getElementById("userName").value;
		
		xmlHttpRequest.open("GET", url, true);
		xmlHttpRequest.send(null);
	}
	
	function callBack() {
		if(xmlHttpRequest.readyState==4 && xmlHttpRequest.status==200) {
			var result = xmlHttpRequest.responseText;
			//...
		}
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics