<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
}
發送一個簡單的HTTP請求。我們用JavaScript請求一個XML檔案,simpleResponse.xml,檔案的常值內容為”Hello from the server!”,然後我們”alert()”simpleResponse.xml檔案的內容。
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“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”>
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject(”Microsoft.XMLHTTP”);
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}