标准XMLHttpRequest方法与属性
标准XMLHttpRequest方法
|
方法 |
描述 |
|
Abort() |
停止当前请求 |
|
getResponseHeader(“header”) |
返回指定首部的串值 |
|
Open(“method”,”url”) |
建立对服务器的调用,method参数可以是GET,POST或PUT。url参数可以是相对URL或绝对URL。这个方法还包括3个可选参数。 |
|
Send(content) |
向服务器发送请求 |
|
setRequestHeader(“header”,”value”) |
把指定首部设置为所提供的值。在设置任何首部之前必须先调用open() |
标准XMLHttpRequest属性
|
属性 |
描述 |
|
Onreadystatechange |
每个状态改变时,都会出发这个事件处理器。通常会调用一个JavaScript函数。 |
|
readyState |
请求的状态。有5个可取值:0=未初始化,1=正在加载,2=已加载,3=交互中,4=完成 |
|
responseText |
服务器的响应,表示为一个串。 |
|
responseXML |
服务器的响应,表示为XML,这个对象可以解析为一个DOM对象。 |
|
Status |
服务器的HTTP状态码(200对应OK,404对应Not Found(未找到),等等) |
|
statusText |
HTTP状态的相应文本(OK或者Not Found(未找到)等等) |
//下面两行使浏览器不会在本地缓存结果
Response.setHeader(“Cache-Control”,”no-cache”);
Response.setHeader(“Pragma”,”no-cache”);
//标准XMLHttpReques应用实例
1
<script type=”text/javascript”>
2![]()
3
Var xmlHttp;
4![]()
5
Function createXMLHttpRequest(){
6![]()
7
If(window.ActiveXObject){
8![]()
9
xmlHttp = new XMLHttpRequest();
10![]()
11
}
12![]()
13
}
14![]()
15
Function startRequest(){
16![]()
17
createXMLHttpRequest();
18![]()
19
xmlHttp.onreadystatechange = handleStateChange;
20![]()
21
xmlHttp.open(“GET”,”innerHTML.xml”,true);
22![]()
23
xmlHttp.send(null);
24![]()
25
}
26![]()
27
Function handleStateChange(){
28![]()
29
If(xmlHttp.readyState == 4){
30![]()
31
If(xmlHttp.status == 200){
32![]()
33
Document.getElementById(“results”).innerHTML = xmlHttp.responseText;
34![]()
35
}
36![]()
37
}
38![]()
39
}
40![]()
41
<script>
42![]()
<script type=”text/javascript”>2

3
Var xmlHttp;4

5
Function createXMLHttpRequest(){6

7
If(window.ActiveXObject){8

9
xmlHttp = new XMLHttpRequest();10

11
}12

13
}14

15
Function startRequest(){16

17
createXMLHttpRequest();18

19
xmlHttp.onreadystatechange = handleStateChange;20

21
xmlHttp.open(“GET”,”innerHTML.xml”,true);22

23
xmlHttp.send(null);24

25
}26

27
Function handleStateChange(){28

29
If(xmlHttp.readyState == 4){30

31
If(xmlHttp.status == 200){32

33
Document.getElementById(“results”).innerHTML = xmlHttp.responseText;34

35
}36

37
}38

39
}40

41
<script>42

Function createXMLHttpRequest()
If(window.ActiveXObject)
浙公网安备 33010602011771号