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