日期:2014-05-18  浏览次数:20857 次

Ajax onreadystatechange问题
<script type="text/javascript">  
var xmlHttp;  
//创建一个XmlHttpRequeset对象  
  if(window.ActiveXObject)...{  
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
  }  
  else if(window.XMLHttpRequest)...{  
  xmlHttp = new XMLHttpRequest();  
  }  
 
//开始一个请求  
function startRequest()...{  
   
  xmlHttp.onreadystatechange = handlestatechange;  
  xmlHttp.open("GET", "SimpleRespose.aspx", true);  
  xmlHttp.Send(null);  
}  
  
function handlestatechange()...{  
  if(xmlHttp.readyState == 4)...{//描述一种"已加载"状态;此时,响应已经被完全接收。  
  if(xmlHttp.status == 200)...{//200表示成功收到  
  alert("The Server Replied with:" + xmlHttp.responseText)  
  }  
  }  
}  
</script>  

现在用一个window.interval("startRequest()",10000)每隔10秒就调用一次
startRequest函数。 在SimpleRespose.aspx的load事件里面每次都response回一个字符串。
但是只有第一次调用startRequest函数的时候才回调用handlestatechange(),这是什么原因呢?按理来说应该每open()一下,当服务器返回时就调用handlestatechange啊

------解决方案--------------------
if(window.ActiveXObject)...{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)...{
xmlHttp = new XMLHttpRequest();
}
这段代码每次都要执行吧。