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)接受響應數據