Jump to content

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


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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...