ajax-----readyState总结

<!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>
虽然现在走得很慢,但不会一直这么慢
原文地址:https://www.cnblogs.com/xxm980617/p/10460107.html