使用php的Ajax,获取状态200,但readyState为0


Ajax with php, getting status 200 but readyState 0

脚本:

function ajaxHandler() {
    var xmlhttp;
    try {  // Opera 8.0+, Firefox, Safari
       xmlhttp = new XMLHttpRequest();
    } catch (e) {   // Internet Explorer Browsers
   try {
        alert("paososdao");
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      try{ 
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {   // Something went wrong
         alert("Your browser broke!");
         return false;
      }
   }
 }
    xmlhttp.onreadystatechange = useHttpResponse();
    xmlhttp.open("GET","prova.php",true); 
    xmlhttp.send(null);
    function useHttpResponse(){ 
        if (xmlhttp.readyState < 4)                         // while waiting response from server
            document.getElementById('test').innerHTML = "Loading...";
        else if (xmlhttp.readyState === 4) {             
            if (xmlhttp.status == 200 && xmlhttp.status < 300)
                document.getElementById('test').innerHTML = xmlhttp.responseText;
       }
    }
}

我使用xampp测试它,我总是得到readyState = 0,prova.php没有响应,但如果我在Chrome上打开开发人员控制台,我可以看到get请求状态是200。出了什么问题。

试试看:

 xmlhttp.onreadystatechange = useHttpResponse;

在需要useHttpResponse之前,您正在调用它。