ajax請求數據的步驟:
1、創建ajax對象
2、配置請求方式和請求地址以及是否異步請求
3、瀏覽器向服務器發送請求
4、服務器接受請求
5、判斷請求並響應數據
6、服務器向瀏覽器返回數據
7、瀏覽器渲染數據
// 1、 if (window.XMLHttpRequest) { var xhr = new XMLHttpRequest(); console.log(xhr); console.log(xhr.constructor); } else { // IE6- var xhr = new ActiveXObject(); } // 2、 // open("請求的方式","請求的地址",是否異步) ---后端人員給提供的 // true 異步 false 同步 默認是異步 xhr.open("GET", "ajax_info.txt", true); // 3、 xhr.send(); // 4、 xhr.onreadystatechange = function () { console.log(xhr); console.log(xhr.readyState);//4 console.log(xhr.status);//200 // 5、 if (xhr.readyState == 4 && xhr.status == 200) { // 6、 console.log(xhr.response); // 7、 document.getElementById("box").innerText = xhr.response; } }