The XMLHttpRequest object makes AJAX possible.


The XMLHttpRequest

To create AJAX web applications you have to become familiar with the JavaScript object called the XMLHttpRequest.
要建立AJAXweb應用程序你就必須開始熟悉JS對象中的XMLHttpRequest

The XMLHttpRequest object is the key to AJAX. It has been available ever since Internet Explorer 5.5 was released in July 2000, but not fully discovered before people started to talk about AJAX and Web 2.0 in 2005.
XMLHttpRequest 對象是AJAX的關鍵。它其實早在2000年的七月便開始生效了,但直到2005年人們開始談論AJAX和Web2.0后才被發掘。

Below is listed some of the methods and properties you have to become familiar with.
下面列舉的是一些方法和屬性,這些你都應該非常熟悉


建立XMLHttpRequest對象

Different browsers use different methods to create an XMLHttpRequest object.
不同的瀏覽器使用了不同的建立方法

Internet Explorer uses an ActiveXObject.
在IE里使用的是ActiveXObject

Other browsers uses a built in JavaScript object called XMLHttpRequest.
其它瀏覽器使用的是JS內建的XMLHttpRequest 對象

Here is the simplest code you can use overcome this problem:
這是段簡單的代碼,可以用來解決這個問題:

var XMLHttp=null
if (window.XMLHttpRequest)
{
XMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
XMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}

Example above explained:
上面舉例的解析:

First create a variable XMLHttp to use as your XMLHttpRequest object. Set the value to null.
首先建立的是一個名為XMLHttp的變量,我們可以用它來代替XMLHttpRequest對象。設置這個變量的值為null

Then test if the object window.XMLHttpRequest is available. This object is available in newer versions of browsers like Firefox, Mozilla, and Opera.
然后測試下對象window.XMLHttpRequest是否有效。這個對象只在Firefox, Mozilla, 和 Opera這些瀏覽器的新版本中有效。

If it's available, use it to create a new object:
如果它是有效的,那么就使用它來建立一個新的對象:

XMLHttp=new XMLHttpRequest().

If it's not available,? test if an object window.ActiveXObject is available. This object is available in Internet Explorer version 5.5 and later.
如果無效。測試下對象window.ActiveXObject是否有效。這個對象只在IE5.5以上的版本中有效。

If it is available, use it to create a new object:
如果有效,那么建立一個新的對象:

XMLHttp=new ActiveXObject().


更好一點的舉例?

Some programmers will prefer to use the newest and fastest version of the XMLHttpRequest object.
一些程序希望使用最新最快版本的XMLHttpRequest對象。

The example below tries to load Microsoft's the latest version "Msxml2.XMLHTTP", available in Internet Explorer 6, before it falls back to "Microsoft.XMLHTTP", available in Internet Explorer 5.5 and later.
下面這個舉例會嘗試加載微軟最新版本中的"Msxml2.XMLHTTP"。它在IE6中有效,以前的版本都是"Microsoft.XMLHTTP"。

var XMLHttp=null
try
{
XMLHttp=new ActiveXObject("Msxml2.XMLHTTP")
}
catch(e)
{
try
{
XMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
}

if (XMLHttp==null)
{
XMLHttp=new XMLHttpRequest()
}

Example above explained:
上面舉例的解析:

First create a variable XMLHttp to use as your XMLHttpRequest object. Set the value to null.
首先建立一個XMLHttp變量可用做你的XMLHttpRequest對象。設置為null值

Then try to create the object the Microsoft way, available in Internet Explorer 6 and later:
然后嘗試用MS的方法來建立一個在IE6或更新版本中有效的對象:

XMLHttp=new ActiveXObject("Msxml2.XMLHTTP")

If this catches an error, try the older (Internet Explorer 5.5) way:
如果捕獲到了錯誤,就嘗試用老一點的方法:

XMLHttp=new ActiveXObject("Microsoft.XMLHTTP")

If? XMLHttp still has a null value, try to create the object the "standard" way:
如果XMLHttp依舊為null值,那么就用“標準”方法建立:

XMLHttp=new XMLHttpRequest()



XMLHttpRequest 方法

open() 方法.

The open() method sets up a request to a web server.
建立對web服務器的請求。

send() 方法.

The send() method sends a request to the server.
向服務器發送請求

abort() 方法.

The abort() method aborts the current server request.
放棄當前的服務器請求


XMLHttpRequest readyState 屬性

The readyState property defines the current state of the XMLHttpRequest object.
readyState 定義了當前XMLHttpRequest對象的狀態

Here are the possible values for the readyState propery:
這所列舉的是一些readyState可能出現的值以及相應的描述:

State
狀態
描述
0 The request is not initialized
請求尚未初始化
1 The request has been set up
請求已經建立
2 The request has been sent
請求已發送
3 The request is in process
請求正在處理中
4 The request is completed
請求完成

readyState=0 after you have created the XMLHttpRequest object, but before you have called the open() method.
發生在你已經建立XMLHttpRequest對象之后,而在你調用open()方法之前的這個階段

readyState=1 after you have called the open() method, but before you have called send().
發生在你已經調用open()方法之后,而在你調用send()方法之前的這個階段

readyState=2 after you have called send().
發生在你已經調用send()方法之后

readyState=3 after the browser has established a communication with the server, but before the server has completed the response.
發生在瀏覽器已經于服務器建立溝通之后,而在服務器完成反饋前的這個階段

readyState=4 after the request has been completed, and the response data have been completely received from the server.
發生在請求完成并收到來自服務器的反饋信息之后

Different browsers treat the ready state differently. Don't expect all browsers to report all states. Some will not report 0 and 1.
不同的瀏覽器對于不同的狀態處理方式不同。別指望所有瀏覽器都能報告所有狀態。一些瀏覽器不會報告0和1

For Your AJAX applications you will actually only be interested state 4. That is when the request is completed and it is safe use the received data.
在你自己的AJAX程序中事實上你只要注重下狀態4就行了。


XMLHttpRequest responseText 屬性

The responseText property contains the text returned by the server.
responseText屬性包含了從服務器返回的文字信息