1
声明一个xmlhttp变量
2
3
var xmlhttp;
4
try
5
{
6
xmlhttp=new ActiveXObject('Msxml2.XMLHTTP');
7
}
8
catch(e)
9
{
10
try
11
{
12
xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');
13
}
14
catch(e)
15
{
16
try
17
{
18
xmlhttp=new XMLHttpRequest();
19
}
20
catch(e)
21
{
22
}
23
}
24
}
25
26
readyState 值:
27
0 这也是readyState一开始具有的值,表示对象已经建立,但还未初始化,这时尚未调用open方法
28
1 表示open方法已经调用,但尚用调用send方法
29
2 表示send方法已经调用,其他数据未知
30
3 表示请求已经发送成功,正在接收数据库
31
4 表示数据已经接收成功。此时相当于直接使用浏览器打开网页,奖态栏显示“完成”字样
32
33
使用status属性判断请求的结果
34
35
200 请求成功
36
202 请求被接收
37
400 错误的请求
38
404 请求资源未找到
39
500 内部服务器错误,如asp代码错误等
40
41
要是想获取XML的话
42
用responseXML
43
在status==200的时候
44
var xmlobj=xmlhttp.responseXML;
45
var title=xmlobj.getElementsByTagName("title")[0].text;
46
47
要是使用post发送数据,需要设置http头
48
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
49
50
51
xmlHttp.onreadystatechange = function()
52
{
53
if(xmlHttp.readyState==2)
54
{
55
ID.innerHTML = "
正在提交数据";
56
}
57
else if(xmlHttp.readyState==3)
58
{
59
ID.innerHTML = "
数据传送中";
60
}
61
else if(xmlHttp.readyState==4)
62
{
63
ID.innerHTML = "";
64
if(xmlHttp.status==200)
65
{
66
ID.innerHTML = xmlHttp.responseText;
67
}
68
else
69
{
70
result.innerHTML = " 查询错误,请检查输入是否正确";
71
}
72
}
73
else
74
{
75
ID.innerHTML = " 正在查询,请稍后
";
76
}
77
}
78
79
xmlHttp.open("GET", url ,true);
80
xmlHttp.send(null);
81
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81