http 时序

本文介绍了HTTP请求的就绪状态,从0到4的四个阶段,详细解析了XMLHttpRequest对象在不同状态下的变化。通过示例代码展示了如何监听和处理这些状态,以及在Chrome浏览器中的实际运行日志。同时提到了本地IIS服务器的响应时间测试,表明其为毫秒级。

HTTP 就绪状态 
0:请求未初始化(还没有调用 open())。
1:请求已经建立,但是还没有发送(还没有调用 send())。
2:请求已发送,正在处理中(通常现在可以从响应中获取内容头)。
3:请求在处理中;通常响应中已有部分数据可用了,但是服务器还没有完成响应的生成。
4:响应已完成;您可以获取并使用服务器的响应了。


下面代码解析了这一个过程

function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
console.log("xmlhttp.readyState(out):"+xmlhttp.readyState);
xmlhttp.onreadystatechange=function()
  {
/* 延时4s ,readyState会结束0->4的变化成功
$("#myDiv").delay(4000).show(function(){
console.log("xmlhttp.readyState:"+xmlhttp.readyState +"|||" +"xmlhttp.status:"+xmlhttp.status);
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
console.log("type of(xmlhttp.responseText):" +typeof(xmlhttp.responseText));
console.log("xmlhttp.responseText:" +xmlhttp.responseText);    
var giveme = xmlhttp.responseText
    $("#myDiv").text(giveme);
    }
})
 延时4s ,readyState会结束0->4的变化成功 */



    //没有延时执行
var date = new Date().getTime();
console.log(date);
console.log("xmlhttp.readyState:"+xmlhttp.readyState +"|||" +"xmlhttp.status:"+xmlhttp.status);
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
console.log("type of(xmlhttp.responseText):" +typeof(xmlhttp.responseText));
console.log("xmlhttp.responseText:" +xmlhttp.responseText);
    
var giveme = xmlhttp.responseText
    $("#myDiv").text(giveme);
    }
//没有延时执行
  }

xmlhttp.open("GET","demo_get.asp",true);
xmlhttp.send();
}


chrom console.log ----->
xmlhttp.readyState(out):0
1395379999743
xmlhttp.readyState:1|||xmlhttp.status:0
1395379999766
xmlhttp.readyState:2|||xmlhttp.status:200
1395379999767
xmlhttp.readyState:3|||xmlhttp.status:200
1395379999768
xmlhttp.readyState:4|||xmlhttp.status:200
type of(xmlhttp.responseText):string
xmlhttp.responseText:<HTML><HEAD><script type="text/javascript" src="jquery-1.11.0.js"></script><script type="text/javascript" src="jqueryT.js"></script></HEAD><BODY><p>demo_write</p><button type="button" onclick="loadXMLDoc()">ask</button><div id="myDiv"></div></BODY></HTML>


好奇了一下,测试了一下本地的IIs服务器的http响应时间,毫秒级。

参考:

http://www.ibm.com/developerworks/cn/xml/wa-ajaxintro2/
http://www.cnblogs.com/fsjohnhuang/articles/2345653.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值