?

AJAX(Asynchronous JavaScript and 使用XML)??是一種在無(wú)需重新加載??整個(gè)網(wǎng)頁(yè)的情況下,能夠更新部分(′?_?`)網(wǎng)頁(yè)的教程技術(shù),通過(guò)在后臺與服務(wù)器進(jìn)行少量數據交換,圖文AJAX可以使網(wǎng)頁(yè)實(shí)現異步更新,詳解這意味??著(zhù)可以在不影響網(wǎng)頁(yè)?的附視情況下,與服務(wù)器交換數據并更新部分網(wǎng)頁(yè)內容。使用
(圖片來(lái)源網(wǎng)絡(luò ),(′?ω?`)教程侵刪)下面是圖文一┐(′д`)┌個(gè)詳細的AJAX使用教??程圖文詳解:
1、打開(kāi)瀏覽器的詳解開(kāi)發(fā)者工具,輸入以下代碼創(chuàng )建一個(gè)XMLHttpRequest對(dui)象:
var xhr = new XMLHttpRequest();
2、附視XMLHttpRequest對象有以下方法:
open=""(method,使用 url, async):初始化一個(gè)請求,參數method表示請求類(lèi)型,教程如"GET"或"POST";url表示請求的圖文URL;async表示是否異步發(fā)送請求,默認為(°o°)true。詳解
xhr.open=""("GET",附視?? "example.php", true);send():發(fā)送請求,可以傳遞參數??作為請求體。
xhr.send()??;
3、設置回調函數處理響應:
xhr.onreadystatechange = function() { if (xhr.readyS??taヽ(′ー`)ノte == 4 && xhr.status == 200) { // 請求成功,處理響應數據 console.log(xhr.respo??ns(′?`*)eTe??xt); } else if (xhヽ(′▽?zhuān)?/r.readyState == 4) { // 請求失敗,處理錯誤信息 console.log("Error: " + xhr.status); }};1、創(chuàng )建一個(gè)H(′?`*)TML文件,添加一個(gè)按鈕和一個(gè)(???)用于顯示結果的元??素:
<!DOC??TYPE html><html lang(′-ι_-`)="en"><head> <met??a charset=&q??uot;UTF8"> <meta name="viewport" content=&quo(???)t;width=??dev(′_`)icewidth, initialscale=1( ?ヮ?).0"(′▽?zhuān)?;&( ?ヮ?)gt; <title&gヾ(^-^)ノt;AJAX GET請求示例??</title></head><body> <button id="getBtn">獲取(′?`)數據</button> <div id="result"></div> <script src="script.j??s"></script></body></html>
2、創(chuàng )建一個(gè)??JavaScript文件(如上例中的script.js),編寫(xiě)以下代碼:
var btn = document.getElementById("getBtn");var resultDiv = document.getElementById("res(°ロ°) !ult");btn.addEventListener("click", function() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { resultDiv.innerHTML = xhr.responseText; } els??e if (xhr.readyState == 4) { console.log("Error: " + xhr.status); } }; xhr.open="open"("GET", "exam??ple.php", true); xhr.send();});<form id="postForm"> <input type="text" id="username" placeholde(╯‵□′)╯r="用戶(hù)名"> <input type=&qヾ(′?`)?uot;password" id="p(T_T)asswordヾ(′?`)?" placeholder="密碼"> <button type="s??ubmit">提交</button></form>2、修改JavaScript文件,編寫(xiě)以下代碼:
var form = document.getElementById("postForm??");var xhr = new XMLHttpRequest();xhr.onreadystatechange = function() { if (xhr.readyState == 4 &┐(′ー`)┌& xhr.status == 200) { console.log("提交成功!"); } else if (xhr.readyState == 4) { console.log(??"Error: &qu??ot; + xhr.status); }};form.addEventListener("submiヽ(′▽?zhuān)?ノt", function(event) { event.preventDefault(); // 阻止表單默認提交行為,使用AJAX提交數據 var data = { user(′;ω;`)name: document.ge?tElementById((╯‵□′)╯&??quot;username").value??, password: document.getElementById("password").value(′?`)}; xhr.open='open'("POST", "example.php", true); // 修改請求類(lèi)型為POST,并設置請求頭ContentType為application/x(′?ω?`)wwwformurlencoded,以傳輸表單數據(可選) xhr.setRequestHeader("ContentType", "application/xwwwformurlenc?oded")??; // 如果需要發(fā)送表單(???)數據,需要設置請求頭ContentType為application/xwwwformurlencoded,并使用???(yong)encodeURIComponent對表單數據進(jìn)行編碼(可(╯‵□′)╯選) xhr.send(encodeURIComponent(JSON.stringify(d(????)ata))); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeURI(′?_?`)Component進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要(′?`*)發(fā)送表單數據,??需要將表單數據轉(zhuan)換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉?換為JSON字符串,并使用encodeURIC??omponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)??}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據??轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)(jin)行編碼(可選)}??); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeUR??IComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串??ヾ(′?`)?,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數(′?`)據轉換為JSON字符串,并使用en??codeURIComponent進(jìn)行編碼(可選(′_ゝ`))}); // 如果需要發(fā)送表(biao)單數據,需??要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換(???)為JSO??N字符串??,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并(O_O)使??用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需??要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要(yao)發(fā)送表單數據,需要將表單數據轉換為JSON(′-ι_-`)字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,??ヽ(′?`)ノ并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換為JSON字符串,并使用encodeURIComponeヽ(′ー`)ノnt進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數( ?ヮ?)據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送??表單數據,需??要將表單數據轉換為JSON字符串,并使用encodeURI??Component進(jìn)行編??碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單數據轉換┐(′?`)┌為JSON字符串,并使用ヽ(′▽?zhuān)?ノencodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數據,需要將表單??數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)}); // 如果需要發(fā)送表單數┐(′д`)┌據,需要將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選)});(′?_?`) // 如果需要發(fā)送??表單數據,需要??將表單數據轉換為JSON字符串,并使用encodeURIComponent進(jìn)行編碼(可選”); //如果不需要等待服務(wù)器響應完成就繼續執行其他操作(例如動(dòng)畫(huà)),可以使用XMLHt(′ω`)tpRequest對象的abort方法取消請求:xh(′?_?`)r.abort();} else { console.log(&quo??t;Error: " + xhr.status);}'); 友情鏈接:
哈爾濱萬(wàn)隆網(wǎng)絡(luò )科技有限公司漢川時(shí)士網(wǎng)絡(luò )科技有限公司商州財磊網(wǎng)絡(luò )科技有限公司
© 2013-2025.Company name All rights reserved.網(wǎng)站地圖 天津九安特機電工程有限公司-More Templates