什麼是 Ajax?
Ajax(非同步 JavaScript 與 XML)能更新局部網頁資料,而不必重新載入整頁。
如何使用 Ajax?
第一步,建立 XMLHttpRequest 物件,var xmlhttp = new XMLHttpRequest();。這個物件負責與伺服器交換資料。
var xhttp; if (window.XMLHttpRequest) { //現代主流浏覽器 xhttp = new XMLHttpRequest(); } else { // 針對浏覽器,比如IE5或IE6 xhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
第二步,用 XMLHttpRequest 的 open() 與 send() 方法把資源請求送給伺服器。
xmlhttp.open(method,url,async):method 包含 get 與 post,url 是檔案或資源的路徑,async 參數為 true(非同步)或 false(同步)。
xhttp.send(); 用 get 方法把請求送到伺服器。
xhttp.send(string); 用 post 方法把請求送到伺服器。
post 什麼時候該用呢?
(1) 更新檔案或資料庫時。
(2) 送出大量資料時,因為 post 沒有字元數量限制。
(3) 送出使用者輸入的加密資料時。
get 範例:
xhttp.open("GET", "ajax_info.txt", true); xhttp.open("GET", "index.html", true); xhttp.open("GET", "demo_get.asp?t=" + Math.random(), true);xhttp.send();
post 範例
xhttp.open("POST", "demo_post.asp", true); xhttp.send();
post 表單資料需要用 XMLHttpRequest 的 setRequestHeader 方法加上一個 HTTP 標頭。
post 表單範例
xhttp.open("POST", "ajax_test.aspx", true); xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhttp.send("fname=Henry&lname=Ford");
async=true 時,伺服器準備好響應後會執行 onreadystatechange 函式。
xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { document.getElementById("demo").innerHTML = xhttp.responseText; } }; xhttp.open("GET", "index.aspx", true); xhttp.send();
async=false 時就不需要寫 onreadystatechange 函式,直接在 send 後面接上執行碼即可。
xhttp.open("GET", "index.aspx", false); xhttp.send(); document.getElementById("demo").innerHTML = xhttp.responseText;
第三步,用 XMLHttpRequest 的 responseText 或 responseXML 屬性取得伺服器響應。
responseText 取得伺服器響應的字串資料,responseXML 取得 XML 資料。
範例如下:
document.getElementById("demo").innerHTML = xhttp.responseText;
伺服器響應的 XML 資料需要用 XML 物件進行轉換。
範例:
xmlDoc = xhttp.responseXML; txt = ""; x = xmlDoc.getElementsByTagName("ARTIST"); for (i = 0; i < x.length; i++) { txt += x[i].childNodes[0].nodeValue + "<br>"; } document.getElementById("demo").innerHTML = txt;
第四步,onreadystatechange 函式。當把請求送到伺服器後,若想在某個時機執行一些功能,就要靠 onreadystatechange;每當 readyState 改變都會觸發它。
onreadystatechange 屬性存放一個在 readyState 改變時自動被呼叫的函式。
readyState 屬性代表 XMLHttpRequest 的狀態,從 0 到 4 變化:0 表示請求尚未初始化,1 表示伺服器連線成功,2 表示請求已被伺服器接收,3 表示正在處理請求,4 表示請求完成且響應就緒。
status 屬性:200 表示成功響應,404 表示頁面不存在。
在 onreadystatechange 事件中,當 readyState==4 且 status==200 時,代表伺服器響應已就緒。
範例:
function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { document.getElementById("demo").innerHTML = xhttp.responseText; } }; xhttp.open("GET", "ajax_info.txt", true); xhttp.send(); } //函數作為參數調用 <!DOCTYPE html> <html> <body> <p id="demo">Let AJAX change this text.</p> <button type="button" onclick="loadDoc('index.aspx', myFunction)">Change Content </button> <script> function loadDoc(url, cfunc) { var xhttp; xhttp=new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { cfunc(xhttp); } }; xhttp.open("GET", url, true); xhttp.send(); } function myFunction(xhttp) { document.getElementById("demo").innerHTML = xhttp.responseText; } </script> </body> </html>
以上就是 Ajax 使用的四大步驟:建立物件、送出請求、取得響應、監聽狀態。希望對大家有幫助,若有疑問歡迎留言,我會盡快回覆,也感謝大家的支持!