Ajax -- W3School 學習筆記

本博客目的只是簡單記錄學習Ajax過程中的知識點,
內容主要參考來源:W3School Ajax 教程

AJAX:

Asynchronous JavaScript and XML(異步的 JavaScript 和 XML)。
AJAX 不是新的編程語言,而是一種使用現有標準的新方法;
AJAX 是在不重新加載整個頁面的情況下,與服務器交換數據並更新部分網頁的藝術。

AJAX 簡介

AJAX 是一種在無需重新加載整個網頁的情況下,能夠更新部分網頁的技術

AJAX = 異步 JavaScript 和 XML。
AJAX是一種用於創建快速動態網頁的技術。
傳統的網頁如果需要更新內容,必須重載整個頁面。

AJAX - 創建 XMLHttpRequest 對象

XMLHttpRequest 是AJAX 的基礎。所有現代瀏覽器均支持。
XMLHttpRequest 對象用於在後臺與服務器對象交換數據。

創建XMLHttpRequest 對象的語法

variable = new XMLHttpRequest();

XMLHttpRequest 對象用於和服務器交換數據。
把請求發送到服務器,我們使用 XMLHttpRequest對象的open() & send() 方法;

方法 描述
open(method, url, async) 規定請求的類型、URL以及是否異步處理請求
send(string) 將請求發送到服務器

- method: 請求的類型, GET / POST
- url : 文件在服務器上的位置
- async : true(異步) 或 false(同步)
- string : 僅用於POST時

XMLHttpRequest 對象如果要用於AJAX的話,open()方法中的async必須爲true。

GET 還是 POST?
與 POST 相比,GET 更簡單也更快,並且在大部分情況下都能用。
然而,在以下情況中,請使用 POST 請求:
- 無法使用緩存文件(更新服務器上的文件或數據庫)
- 向服務器發送大量數據(POST 沒有數據量限制)
- 發送包含未知字符的用戶輸入時,POST 比 GET 更穩定也更可靠

GET請求

簡單的GET請求

xmlhttp.open("GET","demo.asp?fname=lin&lname=yk3",true);
xmlhttp.send();

POST 請求

簡單的POST請求

xmlhttp.open("POST","demo.asp".true);
xmlhttp.send();

Async = true 時,請規定在響應處於onreadystatechange事件中的就緒狀態時執行的函數:

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","test1.txt",true);
xmlhttp.send();

通過 AJAX,JavaScript 無需等待服務器的響應,而是:
- 在等待服務器響應時執行其他腳本
- 當響應就緒後對響應進行處理

Async = false 時,不要編寫onreadystatechange函數,把代碼放到send() 語句後面即可:

xmlhttp.open("GET","test1.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

我們不推薦使用 async=false,但是對於一些小型的請求,也是可以的。請記住,JavaScript 會等到服務器響應就緒才繼續執行。如果服務器繁忙或緩慢,應用程序會掛起或停止

AJAX - 服務器響應

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

屬性 描述
responseText 獲得字符串形式的響應數據
responseXML 獲得XML形式的響應數據

responseText 屬性
除了XML,都可以使用responseText屬性

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

responseXML 屬性
如果來自服務器的響應是XML,而且需要作爲XML對象進行解析,請使用responseXML屬性。
請求 books.xml 文件:

<!--   Copyright w3school.com.cn  -->
<!--  W3School.com.cn bookstore example  -->
<bookstore>
    <book category="children">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>

    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>

    <book category="web" cover="paperback">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>

    <book category="web">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern</author>
        <author>Per Bothner</author>
        <author>Kurt Cagle</author>
        <author>James Linn</author>
        <author>Vaidyanathan Nagarajan</author>
        <year>2003</year>
        <price>49.99</price>
    </book>
</bookstore>

使用responseXML 屬性並解析:

xmlDoc = xmlhttp.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("title");
for(i = 0; i < x.length; i++) {
    txt = txt + x[i].childNodes[0].nodeValue + "<br />";
}
document.getElementById("myDiv").innerHTML = txt;

完整代碼:

 <html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
var txt,x,i;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    xmlDoc=xmlhttp.responseXML;
    txt="";
    x=xmlDoc.getElementsByTagName("title");
    for (i=0;i<x.length;i++)
      {
      txt=txt + x[i].childNodes[0].nodeValue + "<br />";
      }
    document.getElementById("myDiv").innerHTML=txt;
    }
  }
xmlhttp.open("GET","/example/xmle/books.xml",true);
xmlhttp.send();
}
</script>
</head>

<body>

<h2>My Book Collection:</h2>
<div id="myDiv"></div>
<button type="button" onclick="loadXMLDoc()">獲得我的圖書收藏列表</button>

</body>
</html>

查看結果:
結果1
點擊按鈕後得到結果2

發佈了34 篇原創文章 · 獲贊 25 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章