我正在研究一本书中的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 @R_970_4045@ion.</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不能在本地方框上工作,除非它以某种方式通过域引用.我将脚本上传到服务器,它工作得很好.
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。