2010-12-17 81 views
4

我正在從一本書的Ajax示例工作,而本書中的示例不起作用,我在IE 8和FireFox中嘗試過。 asyncRequest.status返回「未指定的錯誤」。我只是在Ajax中探索,這裏有什麼問題?謝謝。Ajax,XMLHttpRequest狀態未指定錯誤

<html xmlns = "http://www.w3.org/1999/xhtml"> 
<head> 
<style type="text/css"> 
    .box { border: 1px solid black; 
     padding: 10px } 
</style> 
<title>Switch Content Asynchronously</title> 
<script type = "text/javascript" language = "JavaScript"> 
    var asyncRequest; // variable to hold XMLHttpRequest object 

    // set up and send the asynchronous request. 
    function getContent(url) 
    { 
    // attempt to create the XMLHttpRequest and make the request 
    try 
    { 
     asyncRequest = new XMLHttpRequest(); // create request object 

     // register event handler 
     asyncRequest.onreadystatechange = stateChange; 
     asyncRequest.open('GET', url, true); // prepare the request 
     asyncRequest.send(null); // send the request 
    } // end try 
    catch (exception) 
    { 
     alert('Request failed.'); 
    } // end catch 
    } // end function getContent 

    // displays the response data on the page 
    function stateChange() 
    { 
    if (asyncRequest.readyState == 4 && asyncRequest.status == 200) 
    { 
     document.getElementById('contentArea').innerHTML = 
      asyncRequest.responseText; // places text in contentArea 
    } // end if 
    } // end function stateChange 

    // clear the content of the box 
    function clearContent() 
    { 
    document.getElementById('contentArea').innerHTML = ''; 
    } // end function clearContent 
</script> 
</head> 
<body> 
    <h1>Mouse over a book for more information.</h1> 
    <img src = 
     "http://test.deitel.com/examples/iw3htp4/ajax/thumbs/cpphtp6.jpg" 
     onmouseover = 'getContent("cpphtp6.html")' 
     onmouseout = 'clearContent()'/> 
    <img src = 
     "http://test.deitel.com/examples/iw3htp4/ajax/thumbs/iw3htp4.jpg" 
     onmouseover = 'getContent("iw3htp4.html")' 
     onmouseout = 'clearContent()'/> 
    <img src = 
     "http://test.deitel.com/examples/iw3htp4/ajax/thumbs/jhtp7.jpg" 
     onmouseover = 'getContent("jhtp7.html")' 
     onmouseout = 'clearContent()'/> 
    <img src = 
     "http://test.deitel.com/examples/iw3htp4/ajax/thumbs/vbhtp3.jpg" 
     onmouseover = 'getContent("vbhtp3.html")' 
     onmouseout = 'clearContent()'/> 
    <img src = 
     "http://test.deitel.com/examples/iw3htp4/ajax/thumbs/vcsharphtp2.jpg" 
     onmouseover = 'getContent("vcsharphtp2.html")' 
     onmouseout = 'clearContent()'/> 
    <img src = 
     "http://test.deitel.com/examples/iw3htp4/ajax/thumbs/chtp5.jpg" 
     onmouseover = 'getContent("chtp5.html")' 
     onmouseout = 'clearContent()'/> 
    <div class = "box" id = "contentArea">&nbsp;</div> 
</body> 
</html> 

更新:我沒有,我是運行我的本地機器上的這個例子中,原帖提及。出於安全原因(我相信,如果我錯了,請糾正我),Ajax在本地框上不起作用,除非它以某種方式通過域引用。我將腳本上傳到服務器,它工作得很好。

回答

2

看起來你的服務器或者不喜歡這個請求,或者你對這些html文件的權限有問題。調試方法:

asyncRequest.send可能無法接受空值。我會嘗試傳遞一個空字符串:「」

確保您可以在不使用ajax的情況下在瀏覽器中擊中這些html文件。如果你不能,那麼你將不得不弄清楚這些文件權限發生了什麼。

在firefox中,安裝Firebug並使用它來調試帶有斷點的代碼,以便您可以確切地看到它發生的位置。

僅供參考,您的代碼與ie6不兼容。需要檢查ActiveXObject,但如果你不關心ie6你設置。

4

直到readyState = 4(complete),請求狀態在IE中不存在,所以你的檢查應該是兩次檢查。 。 。像這樣嘗試。 。 。

if (req.readyState == 4){ 
    // req is complete (200 for web servers, 0 for local files in IE) 
    if ((req.status == 200)||(req.status == 0)){ 
     // good 
    } else{ 
     // error 
    } 
    } 

也,火狐從來文件//協議,但IE6的錯誤嘗試訪問的狀態,如果readyState爲4不返回4的readyState。 。 。 。仍然在我的一個頁面中找出幾個需要在websever上工作並且在ie6中使用本地文件(文件協議)的鏈接,即8和firefox