【问题标题】:Javascript can't interpret returned xmlJavascript 无法解释返回的 xml
【发布时间】:2013-03-25 09:50:21
【问题描述】:

似乎我的 javascript 没有接收到我的 php 发送回一个 xml 文档。 php代码:

$domtree = new DOMDocument('1.0', 'UTF-8');

/* append it to the document created */
$xmlRoot = $domtree->appendChild($domtree->createElement("root"));

foreach (glob('./img/photos/*.*') as $filename) {
    //echo $filename;
    $xmlRoot->appendChild($domtree->createElement("image",$filename));
}

/* get the xml printed */
echo $domtree->saveXML();

上面代码的输出如下:

<?xml version="1.0" encoding="UTF-8"?>
<root><image>./img/photos/2012-02-26 17.02.12.jpg</image>
<image>./img/photos/2012-03-09 08.21.48.jpg</image>
<image>./img/photos/2012-07-21 14.09.39.jpg</image>
<image>./img/photos/2012-07-25 15.25.17.jpg</image>
<image>./img/photos/2012-08-04 17.54.38.jpg</image>
<image>./img/photos/2012-08-04 23.36.30.jpg</image>
<image>./img/photos/2012-08-06 06.08.43.jpg</image>
<image>./img/photos/2012-08-07 20.57.34.jpg</image>
<image>./img/photos/2012-08-09 20.40.11.jpg</image>
<image>./img/photos/2012-08-25 20.54.05.jpg</image>
<image>./img/photos/2012-09-07 11.19.50.jpg</image>
<image>./img/photos/2012-09-08 15.53.27.jpg</image>
<image>./img/photos/2013-01-30 19.19.16.jpg</image>
<image>./img/photos/2013-01-31 09.48.39.jpg</image></root>

使用 AJAX 调用它,当我调用 AJAXRequest.responseXML 时,我会返回 null。

编辑:AJAX 请求代码:

function requestImages()
{
    request=new XMLHttpRequest();
    request.open("GET", "getPhotos.php");
    request.onreadystatechange=showPhotos;
    request.send();
}

function showPhotos()
{
    if ((request.readyState == 4)) {
        doc=request.responseXML; // This returns null
    }
}

【问题讨论】:

  • 什么是 AJAX 代码?也可以直接调用它写回什么是 php 的输出,这样我们就可以看到 Javascript 试图解析什么。
  • 查看答案here
  • 您的 PHP 脚本是否返回了正确的 Content-Type 标头?请参阅stackoverflow.com/a/3272572/156755 了解更多信息

标签: php javascript xml ajax


【解决方案1】:

如果你没有使用任何第三方库,试试这种代码sn-p(根据你的要求修改)

var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request.responseText, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);

请注意,它使用的是 request.responseText,如果您在标题中添加内容类型为 header("Content-type: text/xml");,它肯定会得到返回的 xml

更新

要解析xml,可以使用下面的代码sn-p:

function parseXml(str) {
  if (window.ActiveXObject) {
    var doc = new ActiveXObject('Microsoft.XMLDOM');
    doc.loadXML(str);
    return doc;
  } else if (window.DOMParser) {
    return (new DOMParser).parseFromString(str, 'text/xml');
  }
}

function doNothing() {} //use this for some processing at run time

【讨论】:

  • 有什么理由不使用 jQuery 并避免浏览器自行嗅探?此外,您没有将 XML 解析为可用格式,只是用字符串回调
  • 是的 jQuery 在这里是最好用的。但我相信@jaysee 没有使用它,所以它现在会增加另一个学习曲线?是的,正在解析 xml 我只是建议了相关代码
  • 我不使用 jQuery 的唯一原因是我不知道它。另外,我宁愿避免使用任何特定于平台的代码(ActiveX 仅适用于 Windows,对吗?)
  • 好的,这行得通。第一个代码 sn-p 为我提供了一个我可以正常使用的 XML 文档。
【解决方案2】:

尝试在 php 中发送内容类型,以便 AJAX 知道这是一个 xml 并对其进行解析(请记住,这必须在任何 echo 之前完成):

header("Content-Type: text/xml");

这也可以直接在 javascript 中强制执行(使用overrideMimeType()),但最好在 php 中完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多