您的位置:首页 > 理论基础 > 计算机网络

XMLHTTPRequest的属性和方法简介

2007-08-29 11:01 706 查看

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




<html xmlns="http://www.w3.org/1999/xhtml">


<head>


<title>XMLHTTPRequest物件的說明DEMO</title>




<script language="javascript" type="text/javascript">...


<!--


var xmlhttp;


// 創建一個XMLHTTPRequest物件




function createXMLHTTPRequext()...{




if(window.ActiveXObject) ...{


xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');


}




else if(window.XMLHTTPRequest)...{


xmlhttp = new XMLHTTPRequest();


}


}


function PostOrder(xmldoc)




...{


createXMLHTTPRequext();




// 方法:open


// 創建一個新的http請求,並指定此請求的方法、URL以及驗證資訊


// 語法:oXMLHttpRequest.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);


// 參數


// bstrMethod


// http方法,例如:POST、GET、PUT及PROPFIND。大小寫不敏感。


// bstrUrl


// 請求的URL位址,可以為絕對位址也可以為相對位址。


// varAsync[可選]


// 布林型,指定此請求是否為非同步方式,默認為true。如果為真,當狀態改變時會調用onreadystatechange屬性指定的回調函數。


// bstrUser[可選]


// 如果伺服器需要驗證,此處指定用戶名,如果未指定,當伺服器需要驗證時,會彈出驗證視窗。


// bstrPassword[可選]


// 驗證資訊中的密碼部分,如果用戶名為空,則此值將被忽略。




// 備註:調用此方法後,可以調用send方法向伺服器發送資料。


xmlhttp.Open("get", "http://localhost/example.htm", false);


// var book = xmlhttp.responseXML.selectSingleNode("//book[@id='bk101']");


// alert(book.xml);






// 屬性:onreadystatechange


// onreadystatechange:指定當readyState屬性改變時的事件處理控制碼


// 語法:oXMLHttpRequest.onreadystatechange = funcMyHandler;


// 如下的例子演示當XMLHTTPRequest物件的readyState屬性改變時調用HandleStateChange函數,


// 當資料接收完畢後(readystate == 4)此頁面上的一個按鈕將被啟動


// 備註:此屬性只寫,為W3C文檔物件模型的擴展.


xmlhttp.onreadystatechange= HandleStateChange;




// 方法:send


// 發送請求到http伺服器並接收回應


// 語法:oXMLHttpRequest.send(varBody);


// 參數:varBody (欲通過此請求發送的資料。)


// 備註:此方法的同步或非同步方式取決於open方法中的bAsync參數,如果bAsync == False,此方法將會等待請求完成或者超時時才會返回,如果bAsync == True,此方法將立即返回。


// This method takes one optional parameter, which is the requestBody to use. The acceptable VARIANT input types are BSTR, SAFEARRAY of UI1 (unsigned bytes), IDispatch to an XML Document Object Model (DOM) object, and IStream *. You can use only chunked encoding (for sending) when sending IStream * input types. The component automatically sets the Content-Length header for all but IStream * input types.


// 如果發送的資料為BSTR,則回應被編碼為utf-8, 必須在適當位置設置一個包含charset的文檔類型頭。


// If the input type is a SAFEARRAY of UI1, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.


// 如果發送的資料為XML DOM object,則回應將被編碼為在xml文檔中聲明的編碼,如果在xml文檔中沒有聲明編碼,則使用默認的UTF-8。


// If the input type is an IStream *, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.


xmlhttp.Send(xmldoc);




// 方法:getAllResponseHeaders


// 獲取回應的所有http頭


// 語法:strValue = oXMLHttpRequest.getAllResponseHeaders();


// 備註:每個http頭名稱和值用冒號分割,並以結束。當send方法完成後才可調用該方法。


alert(xmlhttp.getAllResponseHeaders());


// 方法:getResponseHeader


// 從回應資訊中獲取指定的http頭


// 語法:strValue = oXMLHttpRequest.getResponseHeader(bstrHeader);


// 備註:當send方法成功後才可調用該方法。如果伺服器返回的文檔類型為"text/xml", 則這句話


// xmlhttp.getResponseHeader("Content-Type");將返回字串"text/xml"。可以使用getAllResponseHeaders方法獲取完整的http頭資訊。


alert(xmlhttp.getResponseHeader("Content-Type")); // 輸出http頭中的Content-Type列:當前web伺服器的版本及名稱。






document.frmTest.myButton.disabled = true;


// 方法:abort


// 取消當前請求


// 語法:oXMLHttpRequest.abort();


// 備註:調用此方法後,當前請求返回UNINITIALIZED 狀態。


// xmlhttp.abort();




// 方法:setRequestHeader


// 單獨指定請求的某個http頭


// 語法:oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue);


// 參數:bstrHeader(字串,頭名稱。)


// bstrValue(字串,值。)


// 備註:如果已經存在已此名稱命名的http頭,則覆蓋之。此方法必須在open方法後調用。


// xmlhttp.setRequestHeader(bstrHeader, bstrValue);


}


function HandleStateChange()




...{


// 屬性:readyState


// 返回XMLHTTP請求的當前狀態


// 語法:lValue = oXMLHttpRequest.readyState;


// 備註:變數,此屬性唯讀,狀態用長度為4的整型表示.定義如下:


// 0 (未初始化) 物件已建立,但是尚未初始化(尚未調用open方法)


// 1 (初始化) 物件已建立,尚未調用send方法


// 2 (發送資料) send方法已調用,但是當前的狀態及http頭未知


// 3 (資料傳送中) 已接收部分資料,因為回應及http頭不全,這時通過responseBody和responseText獲取部分資料會出現錯誤,


// 4 (完成) 資料接收完畢,此時可以通過通過responseBody和responseText獲取完整的回應資料




if (xmlhttp.readyState == 4)...{


document.frmTest.myButton.disabled = false;




// 屬性:responseBody


// 返回某一格式的伺服器回應資料


// 語法:strValue = oXMLHttpRequest.responseBody;


// 備註:變數,此屬性唯讀,以unsigned array格式表示直接從伺服器返回的未經解碼的二進位資料。


alert(xmlhttp.responseBody);




// 屬性:responseStream


// 以Ado Stream物件的形式返回回應資訊


// 語法:strValue = oXMLHttpRequest.responseStream;


// 備註:變數,此屬性唯讀,以Ado Stream物件的形式返回回應資訊。


alert(xmlhttp.responseStream);




// 屬性:responseText


// 將回應資訊作為字串返回


// 語法:strValue = oXMLHttpRequest.responseText;


// 備註:變數,此屬性唯讀,將回應資訊作為字串返回。XMLHTTP嘗試將回應資訊解碼為Unicode字串,


// XMLHTTP默認將回應資料的編碼定為UTF-8,如果伺服器返回的資料帶BOM(byte-order mark),XMLHTTP可


// 以解碼任何UCS-2 (big or little endian)或者UCS-4 資料。注意,如果伺服器返回的是xml文檔,此屬


// 性並不處理xml文檔中的編碼聲明。你需要使用responseXML來處理。


alert(xmlhttp.responseText);




// 屬性:responseXML


// 將回應資訊格式化為Xml Document物件並返回


// 語法:var objDispatch = oXMLHttpRequest.responseXML;


// 備註:變數,此屬性唯讀,將回應資訊格式化為Xml Document物件並返回。如果回應資料不是有效的XML文檔,


// 此屬性本身不返回XMLDOMParseError,可以通過處理過的DOMDocument物件獲取錯誤資訊。


alert("Result = " + xmlhttp.responseXML.xml);




// 屬性:status


// 返回當前請求的http狀態碼


// 語法:lValue = oXMLHttpRequest.status;


// 返回值:長整形標準http狀態碼,定義如下:


// Number:Description


// 100:Continue


// 101:Switching protocols


// 200:OK


// 201:Created


// 202:Accepted


// 203:Non-Authoritative Information


// 204:No Content


// 205:Reset Content


// 206:Partial Content


// 300:Multiple Choices


// 301:Moved Permanently


// 302:Found


// 303:See Other


// 304:Not Modified


// 305:Use Proxy


// 307:Temporary Redirect


// 400:Bad Request


// 401:Unauthorized


// 402:Payment Required


// 403:Forbidden


// 404:Not Found


// 405:Method Not Allowed


// 406:Not Acceptable


// 407:Proxy Authentication Required


// 408:Request Timeout


// 409:Conflict


// 410:Gone


// 411:Length Required


// 412:Precondition Failed


// 413:Request Entity Too Large


// 414:Request-URI Too Long


// 415:Unsupported Media Type


// 416:Requested Range Not Suitable


// 417:Expectation Failed


// 500:Internal Server Error


// 501:Not Implemented


// 502:Bad Gateway


// 503:Service Unavailable


// 504:Gateway Timeout


// 505:HTTP Version Not Supported


// 備註:長整形,此屬性唯讀,返回當前請求的http狀態碼,此屬性僅當資料發送並接收完畢後才可獲取。


alert(xmlhttp.status);




// 屬性:statusText


// 返回當前請求的回應行狀態


// 語法:strValue = oXMLHttpRequest.statusText;


// 備註:字串,此屬性唯讀,以BSTR返回當前請求的回應行狀態,此屬性僅當資料發送並接收完畢後才可獲取。


alert(xmlhttp.statusText);


}


}


//-->


</script>


</head>


<body>


<form id="frmTest" action="">


<input id="myButton" type="button" value="Click Me" onclick="PostOrder('http://localhost/example.htm');" />


</form>


</body>


</html>



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: