<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;
}
}
其中XMLHttpRequest对象包含了一些方法以及属性,先不管它们,等用到了再看。
Methods(方法)
abort()
Cancels the current request
取消当前的请求
getAllResponseHeaders()
Returns the complete set of http headers as a string
以字符串的形式返回完整的HTTP头信息
getResponseHeader(”headername”)
Returns the value of the specified http header
返回指定的HTTP头信息值
open(”method”,”URL”,async,”uname”,”pswd”)
Specifies the method, URL, and other optional attributes of a request
为一请求指定发放,URL,和其他的任意属性。
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.
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
setRequestHeader(”label”,”value”)
Adds a label/value pair to the http header to be sent
Properties属性
onreadystatechange* :
An event handler for an event that fires at every state change, typically a call to a JavaScript function.
这个是个最重要的属性,为每次状态的变化而准备的事件处理,往往用于触发一个JavaScript运行。
readyState :
Returns the state of the object:
返回的状态对象:
responseText :
Returns the response as a string
以字符串形式返回
responseXML :
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点树方法和属性来进行解析和检验。
status :
Returns the status as a number (e.g. 404 for “Not Found” or 200 for “OK”)
以数字的形式返回状态(比如404是”没有找到“或200是”好的“)
statusText:
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;
}
}
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
}
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject(”Microsoft.XMLHTTP”);
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}