ajax伺服器響應

來源:互聯網
上載者:User

標籤:ajax

一:伺服器響應

獲得來自伺服器的響應,請使用 XMLHttpRequest 對象的 responseText 或 responseXML 屬性。

屬性                            描述

responseText            獲得字串形式的響應資料。

responseXML            獲得 XML 形式的響應資料。

 

 

responseText 屬性

如果來自伺服器的響應並非 XML,請使用 responseText 屬性。

responseText 屬性返回字串形式的響應,因此您可以這樣使用:

document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

 

responseXML 屬性

如果來自伺服器的響應是 XML,而且需要作為 XML 對象進行解析,請使用 responseXML 屬性:

請求 books.xml 檔案,並解析響應

book.xml

<bookstore>

<book category="children">

<title lang="en">HarryPotter</title>

<author>J K. Rowling</author>

<year>2005</year>

<price>29.99</price>

</book>

<book category="cooking">

<title lang="en">EverydayItalian</title>

<author>Giada DeLaurentiis</author>

<year>2005</year>

<price>30.00</price>

</book>

<book category="web" cover="paperback">

<title lang="en">LearningXML</title>

<author>Erik T. Ray</author>

<year>2003</year>

<price>39.95</price>

</book>

<book category="web">

<title lang="en">XQueryKick Start</title>

<author>James McGovern</author>

<author>Per Bothner</author>

<author>Kurt Cagle</author>

<author>James Linn</author>

<author>VaidyanathanNagarajan</author>

<year>2003</year>

<price>49.99</price>

</book>

</bookstore>

 

ajax指令碼:

 

xmlDoc=xmlhttp.responseXML;

txt="";

x=xmlDoc.getElementsByTagName("ARTIST");

for (i=0;i<x.length;i++)

  {

 txt=txt + x[i].childNodes[0].nodeValue + "<br />";

  }

document.getElementById("myDiv").innerHTML=txt;

 

二:onreadystatechange 事件

當請求被發送到伺服器時,我們需要執行一些基於響應的任務。

每當 readyState 改變時,就會觸發 onreadystatechange 事件。

readyState 屬性存有 XMLHttpRequest 的狀態資訊。

下面是 XMLHttpRequest 對象的三個重要的屬性:

 

屬性                                     描述

onreadystatechange        儲存函數(或函數名),每當readyState 屬性改變時,就會調用該函數。

readyState               存有 XMLHttpRequest 的狀態。從 0 到 4 發生變化。

0: 請求未初始化

1: 伺服器串連已建立

2: 請求已接收

3: 請求處理中

4: 請求已完成,且響應已就緒

status                                   200: "OK"

404: 未找到頁面

 

 

使用 Callback 函數

callback 函數是一種以參數形式傳遞給另一個函數的函數。

如果您的網站上存在多個 AJAX 任務,那麼您應該為建立 XMLHttpRequest 對象編寫一個標準的函數,並為每個 AJAX 任務調用該函數。

 

三:入門案例

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>無標題文檔</title>

</head>

<scripttype="text/javascript">

         //ajax核心對象名稱

         varxmlhttp;

         //建立XMLHttpRequest核心對象

         functioncreateXMLHttp(){

                   //判斷使用的瀏覽器類型

                   if(window.XMLHttpRequest){

                            //支援的瀏覽器類型IE7+, Firefox, Chrome, Opera, Safari

                            xml=newXMLHttpRequest();

                   }

                   else{

                            //支援的瀏覽器類型 IE6,IE5

                            xml=newActiveXObject("Microsoft.XMLHTTP");

                   }

         }

         functionshowMsg(){

                   //建立xmlhttp核心對象

                   createXMLHttp();

                   //設定一個請求

                   xmlhttp.open("get","test1.txt");

                   //佈建要求完成之後處理的回呼函數

                   xmlhttp.onreadystatechange=showMsgCallBack;

                   //發送請求不傳遞任何函數

                   xmlhttp.send(null);

         }

         //定義回呼函數

         functionshowMsgCallBack(){

                   //資料返回完畢

                   if(xmlhttp.readyState==4){

                            //http操作正常

                            if(xmlhttp.status==200){

                                     //接受返回的內容

                                     vartext=xmlhttp.responseText;

                                     //設定使用的css樣式表

                                     document.getElementById("msg").innerHTML=text;

                            }

                   }

         }

</script>

<body>

<input type="button"value="調用ajax顯示內容" onClick="showMsg()">

<spanid="msg"></span>

</body>

</html>

著作權聲明:博主原創文章,轉載請說明出處。http://blog.csdn.net/dzy21

ajax伺服器響應

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.