Ajax 基本使用的四大步驟,簡單易懂

來源:互聯網
上載者:User

標籤:處理   demo   comment   hang   command   containe   open   bar   javascrip   

ajax(非同步javascript xml) 能夠重新整理局部網頁資料而不是重新載入整個網頁。接下來通過本文給大家介紹Ajax的使用四大步驟,非常不錯,感興趣的朋友看下吧

什麼是ajax?

ajax(非同步javascript xml) 能夠重新整理局部網頁資料而不是重新載入整個網頁。

如何使用ajax?

第一步,建立xmlhttprequest對象,var xmlhttp =new XMLHttpRequest();XMLHttpRequest對象用來和伺服器交換資料。

12345678 var xhttp;if (window.XMLHttpRequest) {//現代主流瀏覽器xhttp = new XMLHttpRequest();} else {// 針對瀏覽器,比如IE5或IE6xhttp = 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例子:

 
123 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例子

 
12 xhttp.open("POST", "demo_post.asp", true);xhttp.send();

post表單資料需要使用xmlhttprequest對象的setRequestHeader方法增加一個HTTP頭。

post表單例子

 
123 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函數。

 
1234567 xhttp.onreadystatechange = function() {if (xhttp.readyState == 4 && xhttp.status == 200) {document.getElementById("demo").innerHTML = xhttp.responseText;}};xhttp.open("GET", "index.aspx", true);xhttp.send();

asyn=false 則將不需要寫onreadystatechange函數,直接在send後面寫上執行代碼。

 
123 xhttp.open("GET", "index.aspx", false);xhttp.send();document.getElementById("demo").innerHTML = xhttp.responseText;

第三步,使用xmlhttprequest對象的responseText或responseXML屬性獲得伺服器的響應。

使用responseText屬性得到伺服器響應的字串資料,使用responseXML屬性得到伺服器響應的XML資料。

例子如下:

 
1 document.getElementById("demo").innerHTML = xhttp.responseText;

伺服器響應的XML資料需要使用XML對象進行轉換。

例子:

 
1234567 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函數,每次xmlhttprequest對象的readyState發生改變都會觸發onreadystatechange函數。

onreadystatechange屬性儲存區一個當readyState發生改變時自動被調用的函數。

readyState屬性,XMLHttpRequest對象的狀態,改變從0到4,0代表請求未被初始化,1代表格服務器串連成功,2請求被伺服器接收,3處理請求,4請求完成並且響應準備。
status屬性,200表示成功響應,404表示頁面不存在。

在onreadystatechange事件中,伺服器響應準備的時候發生,當readyState==4和status==200的時候伺服器響應準備。

例子:

 
123456789101112131415161718192021222324252627282930313233343536 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的使用四大步驟

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.