【问题标题】:Using PHP to export XML, pulling it into the DOM via AJAX, but the xml is not visible使用PHP导出XML,通过AJAX拉入DOM,但是xml不可见
【发布时间】:2010-12-19 14:31:41
【问题描述】:

我有一个似乎正在生成有效 XML 输出的 PHP 脚本,我试图通过 ajax XMLHttpRequest 调用将其拉入浏览器。

在进行 ajax 调用后,我可以在 firebug 中看到请求成功并且 xml 看起来有效,但是当我尝试将响应放入变量时,我收到一个错误,说响应为空。

这是 PHP 代码:

<?php
$q=$_GET["q"];

$con = mysql_connect('address.com', 'dbnme', 'password');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("sql01_5789willgil", $con);

$sql="SELECT * FROM place";

$result = mysql_query($sql);

$xmlResponse = "<?xml version='1.0' encoding='ISO-8859-1'?>\n";
$xmlResponse .= "<placeList>\n";

while($row = mysql_fetch_array($result))
  {
  $xmlResponse .= "<place>\n";
  $xmlResponse .= "<title>". $row['title'] . "</title>\n";
  $xmlResponse .= "<description>" . $row['description'] ."</description>\n";
  $xmlResponse .= "<latitude>" . $row['latitude'] ."</latitude>\n";
  $xmlResponse .= "<longitude>" . $row['longitude'] ."</longitude>\n";
  $xmlResponse .= "<image>" . $row['image'] ."</image>\n";
  $xmlResponse .= "</place>\n";
  }

echo $xmlResponse;

mysql_close($con);
?>

还有令人讨厌的 JavaScript:

//Pull in the xml data for the bubbles
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }

        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            xmlDoc=xmlhttp.responseXML;  //pull the xml into the DOM
            var x = xmlDoc.getElementsByTagName("place"); //return the contents of the xml file (places) to an array called x
            setupMap();
            }
          }

        xmlhttp.open("GET","getPlaces.php",true);
        xmlhttp.send();

非常感谢您的帮助!

【问题讨论】:

    标签: php javascript xml ajax


    【解决方案1】:

    你好像忘记关闭你的根节点

    // ensure not output before this point
    header("Content-type: text/xml"); // good practice
    while($row = mysql_fetch_array($result))
    {
      ...;
    }
    
    $xmlResponse .= "</placeList>\n";
    echo $xmlResponse;
    

    为确保成功解析 DOM,可能需要一个额外的标头来指示响应是 XML

    【讨论】:

    • 谢谢!我现在已经包含了它......但仍然得到相同的错误:xmlDoc 为空...... :( 编辑:它完全在我第一次使用 xmlDoc 的行上失败:var x = xmlDoc.getElementsByTagName("place ");
    • 如何在http头中加入header("Content-type: text/xml");
    【解决方案2】:

    也许xml毕竟是无效的。尝试阅读responseText 看看你是否得到任何结果。此外, PHP 有一个 DOM 对象本身用于生成 xml。使用它可以更轻松地创建有效的 XML。

    [编辑]

    据我所知,您至少忘记了输出末尾的&lt;/placelist&gt;

    逃避你的价值观可能是个好主意。标题中的&lt; 会破坏您的 XML。

    【讨论】:

    • 结束 标记的要点。我现在已经包含了它 - 但发生了同样的错误,xmlDoc 为空。将尝试您的其他建议...谢谢!
    • 如果我在 responseText 中阅读并在警报中显示它,它可以工作 - 我可以看到整个响应。但是xml版本还是Null...嗯。
    • 我导出了 responseText,将其保存为 .xml 文件并通过 w3c xml 验证器运行它 - 它通过了 - 所以 XML 绝对有效......
    猜你喜欢
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多