3

我正在尝试我的第一个 AJAX,但我的 xml 接收功能有问题。我提醒 responseText,我可以看到从我的服务器返回的 xml,但是当我尝试获取 responseXML 时,我得到 null 和错误。

这是构建我的 xml 的 php 函数

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

这是我的 javascript 函数,它正在处理返回的 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


    }
}
4

5 回答 5

2

这可能是因为,即使您正确设置了内容类型,您也需要<xml在响应的顶部有一个标签。此外,您没有正确关闭最后一个标签。这应该有效:

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

于 2010-11-29T19:42:03.587 回答
1

您正在不正确地关闭父节点(</results>not <results/>),并且您应该(在对其进行清理之后)将所有 POSTDATA 包装在<![CDATA[...]]>标签中以确保安全。确保它也是 UTF8 编码的(请参阅 参考资料utf8_encode()

编辑:以及 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,不application/xml,去?

于 2010-11-29T19:41:32.057 回答
1

答案是将函数“open()”中的异步属性设置为false。像这样:

ajaxObject.open("POST", "my_XML_Generator.php", false);
ajaxObject.setRequestHeader("Content-type", "text/xml");
ajaxObject.send();
于 2012-07-08T08:14:32.830 回答
0

我有同样的问题,直到我把结果放到子节点中才能解决它,例如

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;
    }
}
于 2013-05-19T17:45:04.180 回答
0

我的团队曾经遇到过这个错误,我花了很长时间才意识到问题出在从服务器返回的 XML 数据中。特别是,生成返回 XML 字符串的 PHP 脚本是罪魁祸首。

我的解决方案是删除 PHP 脚本开头的所有空格。我的意思是任何空格、新行和/或制表符都需要从脚本的开头删除,这样脚本中的第一件事就是<?php标签本身。事实证明,<?PHP标签并不是我的 PHP 脚本第一行的第一件事;不知何故,我在第二行开始了我的代码,而第一个脚本行只是一个空行。

这让我很生气,我花了一些时间才弄清楚这一点,所以我希望其他人可以从这个解决方案中受益。如果所有其他方法都失败了,这非常简单且容易尝试。

于 2014-04-21T22:32:40.477 回答