douduanque5850 2015-02-11 12:06
浏览 70
已采纳

Ajax与php,获得状态200但readyState 0

Script:

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;

       }
    }
}

I'm testing it using xampp and I always get readyState = 0 and no response from prova.php, but if I open the developer console on Chrome I can see that the GET request status is 200. What's the problem.

  • 写回答

1条回答 默认 最新

  • doulang9521 2015-02-11 13:01
    关注

    Try that:

     xmlhttp.onreadystatechange = useHttpResponse;
    

    You're calling the useHttpResponse before you need it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探