ajax readystate

来源:互联网 发布:ibm建筑软件 编辑:程序博客网 时间:2024/05/17 21:39

ajax 在请求时一共有0-4五种状态,

Holds the status of the XMLHttpRequest. Changes from 0 to 4: 
0: request not initialized 
1: server connection established
2: request received 
3: processing request 
4: request finished and response is ready

用原生javascript处理ajax:

xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;    }  }

jquery用suceess, error, complete, beforesend等方法对应xhr的状态,但却没有对应readystate等于3时的函数,

在stackoverflow看到一解决办法,不知可行否

var xhr = $.ajax({ ... });xhr.onreadystatechange = function() { alert(xhr.readyState); };