2015-02-11 45 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

相關問題