<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
//1.創建一個XMLHttpRequest對象,相當於打開一個瀏覽器窗口
var xhr = new XMLHttpRequest();
console.log(xhr.readyState);//=================這裏爲0,表示剛創建這個對象
//2.打開一個與網址之間了連接,相當於在地址欄輸入了網址
xhr.open('GET','/06php/user.php');
//3.發送請求
console.log(xhr.readyState);//=================這裏爲1,表示已經調用了這個方法
xhr.send();
console.log(xhr.readyState);//=================這裏爲1,
//4.指定狀態事件處理函數
xhr.onreadystatechange=function(){
switch(this.readyState){
//readyState=2 //==================這裏爲2,說明接受了響應,能拿到響應頭
case 2:
//能拿到響應頭
console.log(this.getAllResponseHeaders())
//但是拿不到響應體
console.log(this.responseText);
break;
////readyState=3 //===================正在下載這個響應報文,能不能拿到取決於報文大小
case 3:
//可能拿得到,也可能拿不到
console.log(this.responseText);
break;
//readyState=4==========================這裏完全可以拿到,一般我們是 在這裏再處理後面的東西
console.log(this.responseText);
break;
}
};
</script>
</body>
</html>