Ajax error, XMLHttpRequest undefined

I am working on Ajax examples from a book, and the example from the book does not work, I tried this in IE 8 and FireFox. asyncRequest.status returns "Unspecified error". I'm just poking around in Ajax, what's the problem? Thanks.

<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> 

UPDATE: I did not mention in the original post that I used this example on my local machine. For security reasons (I believe, please correct me if I am wrong), Ajax does not work in the local field if it is not associated with any domain. I uploaded the script to the server and it worked fine.

+4
source share
2 answers

It looks like your server either does not like it, or in some way, or something related to your permissions for these html files. Debugging Methods:

asyncRequest.send cannot take a null value. I will try to pass an empty string: ""

Make sure you can delete these HTML files in the browser without using ajax. If you cannot, then you will have to figure out what happens to these file permissions.

In firefox, install Firebug and use this to debug your code using breakpoints so you can see exactly where it is happening.

FYI, your code is incompatible with ie6. You need to check ActiveXObject, although if you are not interested in ie6, you are configured.

+2
source

Request status does not exist in IE until ReadyState = 4 (full), so your check should be two checks., Try it like this.,

  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 } } 

also Firefox never returns readyState of 4 for the // protocol file, but ie6 errors try to access the state if readyState is not 4., still developing several bends on one of my pages that should work in websever and with local files (by file protocol) in ie6, i.e. 8 and firefox

+4
source

Source: https://habr.com/ru/post/1332527/


All Articles