2013-08-20 34 views
0

花費了幾個小時來查找出了什麼問題,但失敗了..因爲我是新手,因此我不知道我在看什麼。真的需要你們找到錯誤。無法讀取未定義的屬性'readyState'

HTML

<!doctype html> 
<head> 
    <script type="text/javascript" src="foodstore.js"></script> 
</head> 

<body onload="process()"> 
    <h3>The Chuff Bucket</h3> 
    Enter the food you would like to order: 
    <input type="text" id="userInput"> 
    <div id="underInput" /> 
</body> 

</html> 

的javascript:

var xmlHttp = createXmlHttpRequestObject(); 

function createXmlHttpRequestObject(){ 
    var xmlHttp; 

    if(window.ActiveXobject){ 
     try{ 
      xmlHttp = new ActiveXobject("Microsoft.XMLHTTP"); 

     }catch(e){ 
      xmlHttp = false; 
     } 
    }else{ 
     try{ 
      xmlHttp = new XmlHttpRequest(); 
     }catch(e){ 
      xmlHttp = false; 
     } 
    } 

    if(!xmlHttp){ 
     alert("can't create that object"); 
    } 
    else{ 
     return xmlHttp; 
    } 
} 

function process(){ 
    if(xmlHttp.readyState==0 || xmlHttp.readyState==4){ 
     food = encodeURIComponent(document.getElementById("userInput").value); 
     xmlHttp.open("GET", "foodstore.php?food=" + food, true); 
     xmlHttp.onreadystatechange = handleServerResponse; 
     xmlHttp.send(null); 
    }else{ 
     setTimeout('process()', 1000); 
    } 

} 

function handleServerResponse(){ 
    if(xmlHttp.readyState==4){ 
     if(xmlHttp.status==200){ 
      xmlResponse = xmlHttp.responseXML; 
      xmlDocumentElement = xmlResponse.documentElement; 
      message = xmlDocumentElement.firstChild.data; 
      document.getElementById("underInput").innerHTML = "<span style='color:blue'>" + message + "</span>"; 
        setTimeout('process()', 1000); 

     }else{ 
      alert('something went wrong'); 
     } 
    } 
} 

PHP(我覺得這個文件引起的問題)

<?php 
header('Content-Type: text/xml'); 
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; 

echo '<response>'; 
    $food = $_GET['food']; 
    $foodArray = array('tuna','bacon','beef','ham'); 

    if(in_array($food,$foodArray)){ 
     echo 'We do have ' .$food. '!'; 
    }else if($food == ''){ 
     echo 'Enter a food name.'; 
    }else 
    { 
     echo "no, we don't sell " .$food. "!"; 
    } 

echo '</response>'; 

?> 
+2

不是一個解決辦法,但你應該永遠* *通字符串'setTimeout'。這使得它使用'eval',這是不好的!傳遞一個函數:'setTimeout(process,1000);' –

+0

我同意Rocket Hazmat關於setTimeout的字符串參數,但除此之外,我沒有看到您的代碼出現問題。所以可能這個錯誤是在別的地方... – devnull69

+0

你的PHP代碼實際上是什麼回聲?你確定它是有效的XML嗎? –

回答

2

JavaScript是區分大小寫的。你有一些語法錯誤是由於一些你想創建對象的外殼不當:

ActiveXobject 

應該

ActiveXObject 
    ^

XmlHttpRequest 

應該

XMLHttpRequest 
^^ 

最終的結果是,重新嘗試創建不存在的事物,導致xmlHttp變量始終爲falseundefined

+1

也'新的XmlHttpRequest'。 'm'和'l'應該是大寫字母。 –

+0

@RocketHazmat:謝謝你指出,我更新了我的答案。 –

+0

仍然無效 –

2

您的對象創建邏輯看起來很落後(應該嘗試儘可能創建現代對象,僅在必要時才執行IE事件),並且大寫錯誤。

嘗試:

if (window.XMLHttpRequest) { 
     try { 
      xmlHttp = new XMLHttpRequest(); 
     } catch(e) { 
      xmlHttp = false; 
     } 
    } else { 
     try{ 
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 

     }catch(e){ 
      xmlHttp = false; 
     } 
    } 
相關問題