使用回调函数监视请求(补充)

<html>
<head>
<script type="text/javascript">
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;//表明请求已经完成
var req;
//sendRequest()函数,告诉请求对象使用哪个回调函数
function sendRequest(url,params,HttpMethod){
 if (!HttpMethod){
  HttpMethod="GET";
 }
 req=getXMLHTTPRequest();
 if (req){
  req.onreadystatechange=onReadyStateChange;
  req.open(HttpMethod,url,true);
  req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  req.send(params);
 }
}
function onReadyStateChange(){
 var ready=req.readyState;
 var data=null;
 if (ready==READY_STATE_COMPLETE){
 data=req.responseText;
 }else{
 data="loading...["+ready+"]";
 }

}

window.onload=function(){
  console=document.getElementById('console');

  sendRequest("data.txt","null","post");
}
</script>
</head>
<body>
<div id='console'></div>
</body>
</html>


 

你可能感兴趣的:(使用回调函数监视请求(补充))