XMLHttpRequest的readyState:
0:未初始化——創建
1:初始化——open
2:發送請求——send
3:開始接受結果
4:接收結果完畢
狀態每改變一次,調用一次回調函數。方法調用5次,但是前兩次的readyState==1
onreadystatechange = callback
綁定回調函數,不加()。
XMLHttpRequest的status:
200 OK
404 Not Found
500 Serevr Error
1 var xmlHttpRequest;
2
3 //創建XMLHttpRequest對象
4 function createXmlHttpRequest() {
5 var xmlHttp;
6 if(window.ActiveXObject) {
7 try {
8 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
9 } catch (e) {
10 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
11 }
12 }else if(window.XMLHttpRequest) {
13 xmlHttp = new XMLHttpRequest()
14 }
15
16 return xmlHttp;
17 }
18
19
20 function checkExist() {
21 xmlHttpRequest = createXmlHttpRequest();
22 xmlHttpRequest.onreadystatechange = callBack;
23
24 var url = "http://localhost:8080/Ajax/CheckUser?uname="+document.getElementById("userName").value;
25
26 xmlHttpRequest.open("GET", url, true);
27 xmlHttpRequest.send(null);
28 }
29
30 function callBack() {
31 if(xmlHttpRequest.readyState==4 && xmlHttpRequest.status==200) {
32 var result = xmlHttpRequest.responseText;
33 //
34 }
35 }
posted on 2011-03-14 15:36
wawlian 閱讀(216)
評論(0) 編輯 收藏 所屬分類:
Ajax