Ajax,XMLHttpRequest状态未指定错误
问题描述:
我正在从一本书的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在本地框上不起作用,除非它以某种方式通过域引用。我将脚本上传到服务器,它工作得很好。
答
看起来你的服务器或者不喜欢这个请求,或者你对这些html文件的权限有问题。调试方法:
asyncRequest.send可能无法接受空值。我会尝试传递一个空字符串:“”
确保您可以在不使用ajax的情况下在浏览器中击中这些html文件。如果你不能,那么你将不得不弄清楚这些文件权限发生了什么。
在firefox中,安装Firebug并使用它来调试带有断点的代码,以便您可以确切地看到它发生的位置。
仅供参考,您的代码与ie6不兼容。需要检查ActiveXObject,但如果你不关心ie6你设置。
答
直到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