2 Replies - 327 Views - Last Post: 28 January 2012 - 04:23 PM

Topic Sponsor:

#1 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 359
  • Joined: 28-November 09

Ajax not returning data in reponseXML

Posted 28 January 2012 - 03:00 PM

Hi I'm trying to get some practice with sending xml over php, but the ajax always puts the xml in the responseText, instead of the responseXML. The alert for the responseXML says [object Document] and the responseText contains the xml

This is the javascript:
 var r;
            function runjs(e)
            {
                
                r = new  XMLHttpRequest();
                
                r.open( "POST","back.php");
                r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                r.onreadystatechange = next;
                r.send("text=" + e.value);
                
                
            }
            function next()
            {
                if(r.readyState == 4)
                {
                    if(r.status == 200){
                        //var xm = r.responseXML;
                        alert(r.responseXML);
                        alert(r.responseText);
                    }
                    
                }
                
            }



And this is the php
<?PHP
  header('Content-Type: text/xml');
  echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n";
?>
<response>
  <command method="setcontent">
    <target>example3</target>
    <content>"!"</content>
  </command>
</response>



I have checked the response headers and it is correctly being set, also directly visiting the php page displays the xml, although it does say "This XML file does not appear to have any style information associated with it. The document tree is shown below." Any help would be much appreciated, this is getting frustrating

This post has been edited by ianian112: 28 January 2012 - 03:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Ajax not returning data in reponseXML

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1758
  • View blog
  • Posts: 2,693
  • Joined: 08-June 10

Re: Ajax not returning data in reponseXML

Posted 28 January 2012 - 03:48 PM

View Postianian112, on 28 January 2012 - 10:00 PM, said:

The alert for the responseXML says [object Document] and the responseText contains the xml

That is exactly how it should be. The responseXML property contains parsed a XML Document that you can traverse, much like the DOM of the page itself. The responseText contains just the raw text from the response.

Using XML data can be a little... verbose. For instance, in order to parse your commands from the responseXML, you would do something along these lines:
// The "xml" param  would be the responseXML from you 
// AJAX query.
function processXML(xml) 
{
    // Get a list of <command> tags and run through them.
    var commands = xml.getElementsByTagName("command");
    if (commands && commands.length > 0) 
    {
        for (var ci = 0; ci < commands.length; ++ci) 
        {
            // Find all <target> elements in this command.
            var target = commands[ci].getElementsByTagName("target");
            if (target && target.length > 0) 
            {
                // This gets the text from within the first 
                // <target> element.
                target = target[0].firstChild.nodeValue;
            }
            else 
            {
                console.log("Command has no target");
                continue;
            }
            
            // Now find the <content> element, the same way
            // we found the <target> element.
            var content = commands[ci].getElementsByTagName("content");
            if (content && content.length > 0) 
            {
                content = content[0].firstChild.nodeValue;
            }
            else 
            {
                console.log("Command has no content");
                continue;
            }
            
            // And print the results!
            console.log("Command on target '" + target + "' with content: '" + content + "'");
        }
    }
}



Personally I find this a little to much trouble, especially when it can be done so much more simply by using JSON instead of XML.
<?PHP
  header('Content-Type: application/json');
  $data = array(
    "command" => array(
        "method" => "setcontent",
        "target" => "example3",
        "content" => "\"!\""
    )
  );
  echo json_encode($data);
?>


function next() {
    if(r.readyState == 4 && r.status == 200) {
        // Parse the JSON data into a Javascript object.
        var data = JSON.parse(r.responseText);
        
        // And then you can just use that directly, without
        // having to traverse the whole XML markup.
        console.log("Command on target '" + data.command.target + "' with content: '" + data.command.content + "'");
    }
}


Was This Post Helpful? 2
  • +
  • -

#3 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 359
  • Joined: 28-November 09

Re: Ajax not returning data in reponseXML

Posted 28 January 2012 - 04:23 PM

Wow thanks a lot for that, the reason I wanted to use XML was it seemed like less code overall, but now I really see that is about the same or more then JSON :) Your post really helped me thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1