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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parsing responseXML

    I'm just getting started with Ajax, and because of an issue that I have trying to get javascript to execute from innerHTML, I wanted to test using XML and parse it into the DOM.

    I've looked at a few examples and it seemed easy enough, but I just can't seem to get it to work.

    Here is what I'm doing.

    I've got this simple PHP page returning some test xml. This would be pulling values from the database in production.

    Code:
    <?php
    header('Content-Type: text/xml');
    ?>
    
    <?xml version="1.0" encoding="utf-8"?>
    
    <response>
    	<h3 id="test" class="success">This is a test.</h3>
    	<p id="normal" class="normal">blah blah blah blah blab blahblah blah blah blah blab blahblah blah blah blah blab blah</p>
    </response>
    Now I have the following javascript, just trying to get at the first node to try to get some data.

    Code:
    function parseXMLResponse(request, target) {
    	if(request.readyState == 4) {
    		if(request.status == 200 || request.status == 304) {
    			alert(request.responseText);
    			var data = request.responseXML;
    			alert(data.getElementsByTagName("response")[0].firstChild.data);
    		} else {
    			alert(request.responseText);
    		}
    	}
    }
    The error I'm receiving is: data.getElementsByTagName("response")[0] has no properties.

    The alert on the responseText shows what appears to be a valid XML document, and you can see that I'm setting the mime-type to text/xml in the php. I'm sending the data using a post from a form because that's how this particular piece will work in production. Could that possibly mess with it? Here is the send code:

    Code:
    function sendData(data,script,target) {
    	var request = getAjaxObject();
    	if(request) {
    		request.onreadystatechange = function() {
    			parseXMLResponse(request,target);
    		};
    		request.open("POST", script, true);
    		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    		request.send(data);
    		return true;
    	} else {
    		return false;
    	}
    }
    I've also tried setting data to data = request.responseXML.documentElement; and I get the same issue. Can anyone give me a tip to what I may be doing wrong? I appreciate the feedback.

    Thanks!
    -Doug

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    If you open up the XML document directly in the browser, does it open up correctly?

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah. I gave that a try earlier just in case the xml was malformed or something. It opened just fine.


  •  

    Posting Permissions

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