当前位置: 代码迷 >> 综合 >> ajax-----readyState总结
  详细解决方案

ajax-----readyState总结

热度:23   发布时间:2023-10-28 18:15:49.0
<!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>

 

  相关解决方案