使用原生的ajax的步驟(五個步驟)


1)創建Ajax引擎對象 XMLHttpRequest

 1 var xhr = new XMLHttpRequest();//創建對象 

2)為Ajax引擎對象綁定監聽(監聽服務器已將數據響應給引擎)

1 //綁定監聽對象
2 xhr.onreadystatechange = function () { 3 //監聽readyState和status
4 if (xhr.readyState == 4 && xhr.status == 200) { 5 var value = xhr.responseText;//獲取數據
6 alert(value); 7 } 8 }

3)綁定提交地址

1 //調用open發方法,
2 //指定請求的路徑,
3 //是否是異步,true:異步
4 xhr.open("get", "/項目名/Servlet文件名?username=" + document.getElementById("username"), true);

4)發送請求

1 //發送請求
2 xhr.send();

5)接受響應數據


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM