944,028 Members | Top Members by Rank

Ad:
Aug 25th, 2006
0

Simple AJAX with xml

Expand Post »
hey. I'm trying to get a grasp on XML for use with AJAX.
here is my code. what i want to do is grab the stuff between the 'content' tags, and display it. then i want to take the stuff between the 'script' tags, and eval it (so it will execute).

here is my simple xml document:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <document>
  3. <myScript> alert("hello world"); </myScript>
  4. <content> This text should be displayed as content. </content>
  5. </document>

and my handleResponse code from the calling page:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function handleResponse(http_request, divID){
  2. var myDiv = document.getElementById(divID);
  3. if (http_request.readyState == 4) {
  4. if (http_request.status == 200) {
  5. myXMLDoc = http_request.responseXML;
  6. content = myXMLDoc.getElementsByTagName("content");
  7. myDiv.innerHTML = content;
  8. } else {
  9. alert("There was a problem with the request.");
  10. }
  11. }
  12. }

When i run this, i get the error "object doesn't support this property or method" ... and its complaining about the line :
content = myXMLDoc.getElementsByTagName("content");

Can anybody see what I'm doing wrong, and possibly suggest how to fix it? I'm just getting into XML, and it would be a big help
Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AndrewSmith is offline Offline
11 posts
since Jul 2006
Aug 27th, 2006
0

Re: Simple AJAX with xml

Your getting that error because that property does not exist on the XML document.

You must first parse the document before you can access the DOM. Once you do this your code should work.

Here is some info on parsing an XML document: http://www.w3schools.com/dom/dom_parser.asp
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
Sailor_Jerry is offline Offline
88 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Date Validation in HTML
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Delay loading advertisements





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC