Jump to content

Ajax Problem


GoodLife

Recommended Posts

I'm new to AJAX and I'm trying to get a simple http request to work. This codes just outputs "0" and never executes anything past xhr.open or triggers a onreadystatechange event. Any help would be great, Thanks!

var xhr = getXMLHttp();document.write(xhr.readyState);xhr.onreadystatechange = function() {document.write(xhr.readyState);};xhr.open('GET', 'file:///test.php', true);xhr.send(null);document.write("ok");function getXMLHttp() {var xhr = false;if (window.XMLHttpRequest) {xhr = new XMLHttpRequest();}else {if (window.ActiveXObject) {try {xhr = new ActiveXObject ("Microsoft.XMLHTTP");}catch (e) { }}}return xhr;}

Link to comment
Share on other sites

1. Get rid of those document.write statements! Once the page loads, document.write clobbers your whole document and REPLACES it. Not what you want. While you're testing, use alerts. Or create a message div and change its innerHTML.2. test.php needs to be on your server. Your desktop probably isn't interpreting PHP.3. For the future, you'll want to build your AJAX object (you call it xhr) in a function. At the moment, you're calling getXMLHttp() before it even exists. (The browser executes the call before it has read the rest of the script.) Might as well set up that function now and trigger it with a button click.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...