什么是ajax?
ajax(異步j(luò)avascript xml) 能夠刷新局部網(wǎng)頁(yè)數(shù)據(jù)而不是重新加載整個(gè)網(wǎng)頁(yè)。
如何使用ajax?
第一步,創(chuàng)建xmlhttprequest對(duì)象,var xmlhttp =new XMLHttpRequest();XMLHttpRequest對(duì)象用來(lái)和服務(wù)器交換數(shù)據(jù)。
var xhttp;
if (window.XMLHttpRequest) {
//現(xiàn)代主流瀏覽器
xhttp = new XMLHttpRequest();
} else {
// 針對(duì)瀏覽器,比如IE5或IE6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
第二步,使用xmlhttprequest對(duì)象的open()和send()方法發(fā)送資源請(qǐng)求給服務(wù)器。
xmlhttp.open(method,url,async) method包括get 和post,url主要是文件或資源的路徑,async參數(shù)為true(代表異步)或者false(代表同步)
xhttp.send();使用get方法發(fā)送請(qǐng)求到服務(wù)器。
xhttp.send(string);使用post方法發(fā)送請(qǐng)求到服務(wù)器。
post 發(fā)送請(qǐng)求什么時(shí)候能夠使用呢?
(1)更新一個(gè)文件或者數(shù)據(jù)庫(kù)的時(shí)候。
(2)發(fā)送大量數(shù)據(jù)到服務(wù)器,因?yàn)閜ost請(qǐng)求沒有字符限制。
(3)發(fā)送用戶輸入的加密數(shù)據(jù)。
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表單數(shù)據(jù)需要使用xmlhttprequest對(duì)象的setRequestHeader方法增加一個(gè)HTTP頭。
post表單例子
xhttp.open("POST", "ajax_test.aspx", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fname=Henrylname=Ford");
async=true 當(dāng)服務(wù)器準(zhǔn)備響應(yīng)時(shí)將執(zhí)行onreadystatechange函數(shù)。
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函數(shù),直接在send后面寫上執(zhí)行代碼。
xhttp.open("GET", "index.aspx", false);
xhttp.send();
document.getElementById("demo").innerHTML = xhttp.responseText;
第三步,使用xmlhttprequest對(duì)象的responseText或responseXML屬性獲得服務(wù)器的響應(yīng)。
使用responseText屬性得到服務(wù)器響應(yīng)的字符串?dāng)?shù)據(jù),使用responseXML屬性得到服務(wù)器響應(yīng)的XML數(shù)據(jù)。
例子如下:
document.getElementById("demo").innerHTML = xhttp.responseText;
服務(wù)器響應(yīng)的XML數(shù)據(jù)需要使用XML對(duì)象進(jìn)行轉(zhuǎn)換。
例子:
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函數(shù),當(dāng)發(fā)送請(qǐng)求到服務(wù)器,我們想要服務(wù)器響應(yīng)執(zhí)行一些功能就需要使用onreadystatechange函數(shù),每次xmlhttprequest對(duì)象的readyState發(fā)生改變都會(huì)觸發(fā)onreadystatechange函數(shù)。
onreadystatechange屬性存儲(chǔ)一個(gè)當(dāng)readyState發(fā)生改變時(shí)自動(dòng)被調(diào)用的函數(shù)。
readyState屬性,XMLHttpRequest對(duì)象的狀態(tài),改變從0到4,0代表請(qǐng)求未被初始化,1代表服務(wù)器連接成功,2請(qǐng)求被服務(wù)器接收,3處理請(qǐng)求,4請(qǐng)求完成并且響應(yīng)準(zhǔn)備。
status屬性,200表示成功響應(yīng),404表示頁(yè)面不存在。
在onreadystatechange事件中,服務(wù)器響應(yīng)準(zhǔn)備的時(shí)候發(fā)生,當(dāng)readyState==4和status==200的時(shí)候服務(wù)器響應(yīng)準(zhǔn)備。
例子:
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();
}
//函數(shù)作為參數(shù)調(diào)用
!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的使用四大步驟,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
您可能感興趣的文章:- jQuery使用ajaxSubmit()提交表單示例
- jquery中ajax使用error調(diào)試錯(cuò)誤的方法
- 基于jquery的$.ajax async使用
- jquery.ajax之beforeSend方法使用介紹
- 使用jquery的ajax需要注意的地方dataType的設(shè)置
- jquery序列化form表單使用ajax提交后處理返回的json數(shù)據(jù)
- 跨域請(qǐng)求之jQuery的ajax jsonp的使用解惑
- Ajax的使用代碼解析
- 淺析Asp.net MVC 中Ajax的使用
- AJAX的使用方法詳解