2015-02-11 48 views
0

脚本:Ajax和PHP,获取状态200,但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.问题是什么。

+1

响应是什么样的?您可以使用Chrome开发工具访问它 – 2015-02-11 12:08:19

回答

0

试一下:

xmlhttp.onreadystatechange = useHttpResponse; 

你调用useHttpResponse你需要它。

+0

非常感谢!有用!我没有想到,使用“useHttpResponse()”而不是“useHttpResponse”应立即调用该函数。 – PaulRox 2015-02-11 13:44:29

+0

不客气;) – 2015-02-11 14:44:22

相关问题