andrew
andrew

Reputation: 5176

Ajax - responseText working but responseXML null

I am trying my first AJAX and having a problem with my xml receiving function. I alert responseText and I can see the xml returned from my server, but when I try and get responseXML I get null and the error.

Here is the php function that builds my xml

  header('Content-type: application/xml');
    echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    echo("<results>");
    echo("<table><![CDATA[tablereererere]]></table>");
    //echo("<ratedTable>".$_POST['ratedTable']."</ratedTable>\n");
    //echo("<table>".$_POST['table']."</table>\n");
    //echo("<post_id>".$_POST['post_id']."</post_id>\n");
    //echo("<user_id>".$_POST['user_id']."</user_id>\n");
    //echo("<rating>".$_POST['rating']."</rating>\n");
    echo("</results>");

And here is my javascript function which is processing the returned xml

function ajaxReceiver(http_request) {

    //this function continues to run until a result is returned and then it creates the new div
    if(http_request.readyState == 4) {

      response_xml = http_request.responseXML;
      response_text =  http_request.responseText;

      alert(response_text);
      alert(response_xml.getElementsByTagName("table")[0].textContent);
      //document.getElementById('floatingNotification').innerHTML = response_text;
       // alert(http_request.responseXML.getElementsByTagName("table")[0].textContent);
      //ratedTable = responseXML.getElementsByTagName("table").value;
      //alert(ratedTable);
      //message = response.getElementsByTagName('table')[0].textContent;
      //alert(message);
     //alert(message);
//this response contains the xml document that was returned by the php function.You can get any values out of the xml document and 
//use javascript dom to manipulate the contents on the page


    }
}

Upvotes: 3

Views: 10772

Answers (5)

axon111
axon111

Reputation: 1

I had this error happen to our team once and it took me a long time before we realized that the problem was in our XML data returned from the server. In particular, PHP script that generated return XML string was the culprit.

My solution was to remove any white space from the beginning of the PHP script. I mean any spaces, new lines, and/or tabs need to be removed from the beginning of the script, so that the first thing in the script is the <?php tag itself. It turned out that the <?PHP tag was not the first thing on the first line of my PHP script; somehow I started my code on the second line and the first script line was simply an empty one.

It drove me mad and it took some time for me to figure this out, so I hope someone else can benefit from this solution. It is very simple and easy to try if all else fails.

Upvotes: 0

Ray
Ray

Reputation: 8844

I had the same problem and couldn't solve it until I put my results into subnodes, e.g.

header("Content-Type: text/xml; charset=utf-8");
echo("<?xml version='1.0' encoding='utf-8'?>\n");
echo("<summary>$summary</summary>\n");
echo("<content>$content</content>\n");

didn't work for me for some weird reason, but this does:

header("Content-Type: text/xml; charset=utf-8");
echo("<?xml version='1.0' encoding='utf-8'?>\n");
echo("<page>\n");
echo("    <summary>$summary</summary>\n");
echo("    <content>$content</content>\n");
echo("</page>\n");

My code retrieving the answer is

function retrieveRequest(title)
{
    if (_xmlRequest.readyState == 4 && _xmlRequest.status == 200)
    {
        var xmlResponse = _xmlRequest.responseXML;
        _divSummary.innerHTML = xmlResponse.getElementsByTagName("summary")[0].textContent;
        _divContent.innerHTML = xmlResponse.getElementsByTagName("content")[0].textContent;
    }
}

Upvotes: 0

karit
karit

Reputation: 19

The answer is to put the asynchronous property in the function "open()" to false. Like this:

ajaxObject.open("POST", "my_XML_Generator.php", false);
ajaxObject.setRequestHeader("Content-type", "text/xml");
ajaxObject.send();

Upvotes: 1

Dereleased
Dereleased

Reputation: 10087

You are closing the parent node improperly (</results> not <results/>) and you should (after sanitizing it) wrap all the POSTDATA in <![CDATA[...]]> tags to be safe. Make sure it's UTF8 encoded, too (see utf8_encode())

EDIT: and what wajiw said about the <?xml version="1.0" encoding="UTF-8" ?> tag at the beginning.

Edit: Example CDATA Block Usage

<?xml version="1.0" encoding="UTF-8" ?>
<myNode>
    <myData><![CDATA[
        Now I just throw in my data, for fun and profit!
        This way I can use special, reserved characters like <, > and &!
    ]]></myData>
</myNode>

Edit Yet Again:

Why not give Content-Type: text/xml, NOT application/xml, a go?

Upvotes: 1

wajiw
wajiw

Reputation: 12269

It may be because, even though you're setting the content-type correctly, you need to have an <xml tag in the top of your response. Also, you aren't closing your last tag properly. This should work:

echo("<?xml version='1.0'?>");
echo("<results>");
echo("<ratedTable>".$_POST['ratedTable']."</ratedTable>");
echo("<table>".$_POST['table']."</table>");
echo("<post_id>".$_POST['post_id']."</post_id>");
echo("<user_id>".$_POST['user_id']."</user_id>");
echo("<rating>".$_POST['rating']."</rating>");
echo("<message>$message</message>");
echo("</results>");

For more info on how to define your XML: http://www.w3.org/TR/REC-xml/#sec-prolog-dtd

Upvotes: 2

Related Questions