Ajax - responseText works, but responseXML is null

I am trying to use my first AJAX and have a problem with my xml receive function. I am warning responseText and I can see the xml returned from my server, but when I try to get responseXML, I get null and an error.

Here is a 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 that processes 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


    }
}
+3
source share
5 answers

Perhaps because, although you have set the content type correctly, you need to have a tag <xmlat the top of your answer. In addition, you are not 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>");

, XML: http://www.w3.org/TR/REC-xml/#sec-prolog-dtd

+2

node (</results>not <results/>), ( ) POSTDATA <![CDATA[...]]>, . , UTF8 (. utf8_encode())

EDIT:, wajiw <?xml version="1.0" encoding="UTF-8" ?> .

: CDATA

<?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>

:

Content-Type: text/xml, NOT application/xml, go?

+1

, "open()" false. :

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

, , , .

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");

- , :

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");

, ,

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;
    }
}
0

, , , XML, . , PHP script, XML-.

PHP script. , , / script, , script <?php. , <?php PHP ; - , script .

It drove me crazy and it took me a while to figure this out, so I hope someone else can take advantage of this solution. It is very simple and easy to try if all else fails.

0
source

Source: https://habr.com/ru/post/1777517/


All Articles