XmlHttpRequest對象 ajax核心之一

來源:互聯網
上載者:User

標籤:頁面   firefox   ref   intern   trie   function   ext   學習   change   

XMLHttpRequest 對象
  • XML XSLT
  • XML 解析器

XMLHttpRequest 對象用於在後台與伺服器交換資料。

什麼是 XMLHttpRequest 對象?

XMLHttpRequest 對象用於在後台與伺服器交換資料。

XMLHttpRequest 對象是開發人員的夢想,因為您能夠:

  • 在不重新載入頁面的情況下更新網頁
  • 在頁面已載入後從伺服器請求資料
  • 在頁面已載入後從伺服器接收資料
  • 在後台向伺服器發送資料

所有現代的瀏覽器都支援 XMLHttpRequest 對象。

如需學習更多有關 XMLHttpRequest 對象的知識,請學習我們的 XML DOM 教程。

執行個體:當鍵入文本時與伺服器進行 XML HTTP 通訊。

建立 XMLHttpRequest 對象

所有現代瀏覽器 (IE7+、Firefox、Chrome、Safari 以及 Opera) 都內建了 XMLHttpRequest 對象。

通過一行簡單的 JavaScript 代碼,我們就可以建立 XMLHttpRequest 對象。

建立 XMLHttpRequest 對象的文法:
xmlhttp=new XMLHttpRequest();
老版本的 Internet Explorer (IE5 和 IE6)使用 ActiveX 對象:
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

提示:在下一章,我們將使用 XMLHttpRequest 對象從伺服器取回 XML 資訊。

執行個體 1
<script type="text/javascript">var xmlhttp;function loadXMLDoc(url){xmlhttp=null;if (window.XMLHttpRequest)  {// code for all new browsers  xmlhttp=new XMLHttpRequest();  }else if (window.ActiveXObject)  {// code for IE5 and IE6  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }if (xmlhttp!=null)  {  xmlhttp.onreadystatechange=state_Change;  xmlhttp.open("GET",url,true);  xmlhttp.send(null);  }else  {  alert("Your browser does not support XMLHTTP.");  }}function state_Change(){if (xmlhttp.readyState==4)  {// 4 = "loaded"  if (xmlhttp.status==200)    {// 200 = OK    // ...our code here...    }  else    {    alert("Problem retrieving XML data");    }  }}</script>

XmlHttpRequest對象 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.