来 自MSDN的解释:XmlHttp提供客户端同http服务器通讯的协议。客户端可以通过XmlHttp对象(MSXML2.XMLHTTP.3.0)向 http服务器发送请求并使用微软XML文档对象模型Microsoft® XML Document Object Model (DOM)处理回应。
XmlHttp对象参考:
属性:
onreadystatechange* | 指定当readyState属性改变时的事件处理句柄。只写 |
readyState | 返回当前请求的状态,只读. |
responseBody | 将回应信息正文以unsigned byte数组形式返回.只读 |
responseStream | 以Ado Stream对象的形式返回响应信息。只读 |
responseText | 将响应信息作为字符串返回.只读 |
responseXML | 将响应信息格式化为Xml Document对象并返回,只读 |
status | 返回当前请求的http状态码.只读 |
statusText | 返回当前请求的响应行状态,只读 |
方法:
abort | 取消当前请求 |
getAllResponseHeaders | 获取响应的所有http头 |
getResponseHeader | 从响应信息中获取指定的http头 |
open | 创建一个新的http请求,并指定此请求的方法、URL以及验证信息(用户名/密码) |
send | 发送请求到http服务器并接收回应 |
setRequestHeader | 单独指定请求的某个http头 |
事件:
无-------------------------
--------------------------
--------------------------
详细说明::
<script type=”text/javascript”>
var xmlHttp;
function creatXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject(”Microsoft.XMLHTTP”);
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else {
return;
}
}
abort()
Cancels the current request
getAllResponseHeaders()
Returns the complete set of http headers as a string
getResponseHeader(”headername”)
Returns the value of the specified http header
open(”method”,”URL”,async,”uname”,”pswd”)
Specifies the method, URL, and other optional attributes of a request
The method parameter can have a value of “GET”, “POST”, or “PUT” (use “GET” when requesting data and use “POST” when sending data (especially if the length of the data is greater than 512 bytes.
The URL parameter may be either a relative or complete URL.
The async parameter specifies whether the request should be handled asynchronously or not. true means that script processing carries on after the send() method, without waiting for a response. false means that the script waits for a response before continuing script processing
send(content)
Sends the request
setRequestHeader(”label”,”value”)
Adds a label/value pair to the http header to be sent
An event handler for an event that fires at every state change, typically a call to a JavaScript function.
这个是个最重要的属性,为每次状态的变化而准备的事件处理,往往用于触发一个JavaScript运行。
Returns the state of the object:
返回的状态对象:
- 0 = uninitialized[初始化]
- 1 = loading[加载中]
- 2 = loaded[加载完毕]
- 3 = interactive[交互]
- 4 = complete [完毕]
Returns the response as a string
以字符串形式返回
Returns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties
以XML的形式返回,这个属性返回一XML文档对象,可用W3C的DOM点树方法和属性来进行解析和检验。
Returns the status as a number (e.g. 404 for “Not Found” or 200 for “OK”)
以数字的形式返回状态(比如404是”没有找到“或200是”好的“)
Returns the status as a string (e.g. “Not Found” or “OK”)
以字符串形式返回状态(比如”没有找到“或”好的“)
<script type=”text/javascript”>
var xmlHttp;
function creatXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject(”Microsoft.XMLHTTP”);
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else {
return;
}
}
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open(”GET”, “simpleResponse.xml”, true);
xmlHttp.send(null);
if (XMLHttp.readyState == 4) {
// everything is good, the response is received
} else {
// still not ready
}
- 0 (未初始化)
- 1 (正在装载)
- 2 (装载完毕)
- 3 (交互中)
- 4 (完成)
if (XMLHttp.status == 200) {
// perfect!
} else {
// there was a problem with the request,
// for example the response may be a 404 (Not Found)
// or 500 (Internal Server Error) response codes
}
xmlHttp.responseText – 以文本字符串的方式返回服务器的响应
xmlHttp.responseXML – 以XMLDocument对象方式返回响应。处理XMLDocument对象可以用JavaScript DOM函数
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
alert(”The server repilied with: ” + xmlHttp.responseText);
}
}
}
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Simple XMLHttpRequest</title>
<script type=”text/javascript”>
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject(”Microsoft.XMLHTTP”);
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open(”GET”, “simpleResponse.xml”, true);
xmlHttp.send(null);
}
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
alert(”The server replied with: ” + xmlHttp.responseText);
}
}
}
</script>
</head>
<body>
<form action=”#”>
<input type=”button” value=”Start Basic Asynchronous Request” onclick=”startRequest();”/>
</form>
</body>
</html>