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
<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; } }2. 接下来要决定当收到服务器的响应后,需要做什么。这需要告诉HTTP请求对象用哪一个JavaScript函数处理这个响应。可以将对象的onreadystatechange属性设置为要使用的JavaScript的函数名。
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
}
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); } } }
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Simple XMLHttpRequest</title> <script type="text/javascript"> var xmlHttp; function createXMLHttpRequest(){ if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } } function startRequest(){ createXMLHttpRequest();//创建XMLHttpRequest xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open("GET", "simpleResponse.xml", true); xmlHttp.send(null); } function handleStateChange(){ 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>