xmlHttpRequest参数
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![]()
声明一个xmlhttp变量2

3
var xmlhttp;4
try5
{6
xmlhttp=new ActiveXObject('Msxml2.XMLHTTP');7
}8
catch(e)9
{10
try11
{12
xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');13
}14
catch(e)15
{16
try17
{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
用responseXML43
在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
else69
{70
result.innerHTML = " 查询错误,请检查输入是否正确";71
}72
}73
else74
{75
ID.innerHTML = " 正在查询,请稍后
";76
}77
}78

79
xmlHttp.open("GET", url ,true);80
xmlHttp.send(null);81

浙公网安备 33010602011771号