Ajax,XMLHttpRequest状态未指定错误
发布时间:2020-12-16 02:50:45 所属栏目:百科 来源:网络整理
导读:我正在研究一本书中的Ajax示例,本书中的示例不起作用,我在IE 8和FireFox中尝试过. asyncRequest.status返回“未指定的错误”.我只是在Ajax中闲逛,这里有什么问题?谢谢. html xmlns = "http://www.w3.org/1999/xhtml"headstyle type="text/css" .box { borde
我正在研究一本书中的Ajax示例,本书中的示例不起作用,我在IE 8和FireFox中尝试过. asyncRequest.status返回“未指定的错误”.我只是在Ajax中闲逛,这里有什么问题?谢谢.
<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你已经设置好了. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |