Jump to content

AJAX is not working smoothely....or at all


jblevins1991

Recommended Posts

I have an xml blog and a javascript file i am trying to use to publish this blog to a website with. I am just testing to see if AJAX is working at all because i have been having issues with this for a day now. Here is the Javascript code: function blog(){var xhr = new XMLHttpRequest(); xhr.onReadyStateChange = function(){if(xhr.readyState == 4 && xhr.status == 200){var xmldom = xhr.responseXML;alert(xmldom.documentElement);}} xhr.open("GET","blog.xml",true);xhr.send();} Here is the xml code: <?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><item><title>hello</title> <link>www.google.com</link> <description>world</description></item><item><title>hello</title> <link>www.google.com</link> <description>world</description></item><item><title>hello</title> <link>www.google.com</link> <description>world</description></item></channel> </rss> Finally, here is the HTML code: <div id="blog"><h4>News Updates</h4><script type="text/javascript" src="blog.js" async></script><button type="button" onclick="blog()">click me</button></div> I have it set up to where the button in the HTML code has the onclick event listener that calls the javascript "blog()" function. the problem is that i never does anything NO MATTER WHAT I DO. it is always unresponsive. I never get a single alert box no matter what i make the script do inside the onreadystate function.

Edited by jblevins1991
Link to comment
Share on other sites

have you tried checking your error console? also, why is async here?

<script type="text/javascript" src="blog.js" async></script>

Edited by thescientist
Link to comment
Share on other sites

google it for the browser you are using.
I see what you mean i have edited this post to refute my previous statement. It says that the file returned a status of 200 and is shown in the network section of the console in chrome. since i know that it is at least getting a ready state of 4 and a status of 200 could it be how i am storing the data into the HTML page? Edited by jblevins1991
Link to comment
Share on other sites

You can use console.log to send a variable to the Javascript console so that you can inspect it. Use console.log to send the xhr variable to the console so that you can check it, you can check the responseXML property to make sure your XML is in there and is structured correctly. Actually, it looks like it is a case problem. "onreadystatechange" should be all lowercase.

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
×
×
  • Create New...