Jump to content

Ajax Problem


GoodLife
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...