Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Mr.
    Mr. is offline
    Regular Coder
    Join Date
    Feb 2011
    Posts
    112
    Thanks
    8
    Thanked 4 Times in 4 Posts

    my first AJAX attempt will not work... why...

    Can you please tell me if you spot a problem.. when I run the page nothing appears.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Simple Ajax</title>
    <script>
    window.onload = makeRequest;
    var xhr = false;
    
    function makeRequest(){
    	if(window.XMLHttpRequest){
    		xhr = new XMLHttpRequest();
    
    	}
    	else {
    		if (window.ActiveXObject) {
    		try {
    			xhr = new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		catch (e) { }
    		}
    	}
    }
    	if (xhr) {
    
    		xhr.onreadystatechange = showState;
    		xhr.open("GET", "states.xml", true);
    		xhr.send(null);
    	}
    	else {
    		document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create a request";	
    	}
    	
    function showState() {
    	var currMsg = document.getElementById("updateArea").innerHTML;
    	document.getElementById("updateArea").innerHTML = currMsg + "<p>The current state is " + xhr.readyState + " and the status is " + xhr.status + "</p>";	
    }
    
    
    </script>
    </head>
    
    <body>
    <div id="updateArea">&nbsp;</div>
    </body>
    </html>
    You can find the XML file at... http://www.testwebdesign.com/states.xml

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Nothing obvious with your code ... you'll either have to debug (Firebug is your friend on Firefox) to find out what's going on or give us a (test) URL where we can observe the behaviour.

  • Users who have thanked devnull69 for this post:

    Mr. (11-04-2011)

  • #3
    Mr.
    Mr. is offline
    Regular Coder
    Join Date
    Feb 2011
    Posts
    112
    Thanks
    8
    Thanked 4 Times in 4 Posts

  • #4
    Mr.
    Mr. is offline
    Regular Coder
    Join Date
    Feb 2011
    Posts
    112
    Thanks
    8
    Thanked 4 Times in 4 Posts
    I got it... it was a misplaced curly bracket. Thanks for the help anyway!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •