【问题标题】:responseXML is nullresponseXML 为空
【发布时间】:2011-10-21 20:08:58
【问题描述】:
url = "http://localhost/xml.php?type=xml";
if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest();
      xmlhttp.open("GET", url, true);
      xmlhttp.setRequestHeader('Content-Type', 'application/xml');
      xmlhttp.send(null);
}
else if (window.ActiveXObject)  {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (xmlhttp) {
        xmlhttp.open("GET", url, true);
        xmlhttp.setRequestHeader('Content-Type', 'application/xml');
        xmlhttp.send();
    }
}

alert(xmlhttp.responseXML); //returns null

XML 文件

<?xml version="1.0" encoding="UTF-8" ?>
<main>
    <food>
        <type>6</type>
        <region>5676</region>
    </food>
    <food>
        <type>6</type>
        <region>5676</region>
    </food>

</main>

有人知道为什么xmlhttp.responseXML 返回为空吗?

【问题讨论】:

  • responseXML always null的可能重复
  • 在另一个问题中,我认为MooGoo answer 与您的情况更相关..
  • 我不知道这是否会导致 responseXML 为空,但您的 XML 无效。您正在使用两个不同的标签来打开/关闭:&lt;type&gt;6&lt;/clotheID&gt;.

标签: javascript ajax


【解决方案1】:

您的 HTTP 请求是异步的。 xmlhttp.responseXMLxmlhttp.readyState 具有4 的值之前不会有一些值。

var url = "http://localhost/xml.php?type=xml";
var xmlhttp;
if (window.XMLHttpRequest) {
      xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)  {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp) {
    xmlhttp.open("GET", url, true);
    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            alert(xmlhttp.responseXML);
        }
    };
    xmlhttp.send();
}

另外,我认为您不需要setRequestHeader 行。响应需要 XML MIME 类型,而不是请求。另外,请尊重良好的编码习惯(不要忘记var、DRY 等)

【讨论】:

【解决方案2】:

确保您的 PHP 脚本中有 header('Content-type: application/xml');。 此外,检查 responseText - 也许你有错误?

【讨论】:

  • 我知道这有点晚了,但我在将文件从一个域迁移到下一个域时遇到了这个问题。我发现在我的 xml 文件的开头创建了一个空格,这给了我一个 readyState 2 但一个 200 状态导致 responseXML 为空,而 response 和 responseText 有一个值。
【解决方案3】:

最近,我从 Apache 迁移到 nginx 并遇到了同样的问题。当作为简单文件或从 Apache 服务器加载时,一切正常,但在 nginx 上运行时,responseXML 始终为空。

我的特殊情况还涉及到一个 XSL 样式表来转换 XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="main-template-transformer.xsl"?>

常规 XML 文件的内容类型恢复得很好。然而,重要的是 XSL 文件的内容类型。 (这是通过检查responseText 发现的,该responseText 不为空,并且包含 XSL 文件的整个文本。检查此文件上的 HTTP 标头显示内容类型在 Apache 和 nginx 之间发生了变化。)

内容类型应为text/xmlapplication/xml。 nginx 1.10.3 中默认为application/octet-stream,这会导致responseXML 始终为空。

这可以通过在 JavaScript 文件中添加以下行来解决:

xmlhttp.overrideMimeType('text/xml');

这可以通过在“conf/mime.types”中的 nginx 服务器配置中添加以下行来解决:

    text/xml                              xsl;

【讨论】:

    【解决方案4】:

    我刚刚测试并找到了解决方案。

    我不知道为什么,但是当您发送 xml 标头时,XMLHttpRequest 无法解析它。

    要使用 XMLHttpRequest 的 DOM responseXML 属性,您必须删除 xml 标头。

    在您的情况下,xml 响应将是

    <main>
        <food>
            <type>6</type>
            <region>5676</region>
        </food>
        <food>
            <type>6</type>
            <region>5676</region>
        </food>
    </main>
    

    【讨论】:

      猜你喜欢
      • 2015-09-05
      • 1970-01-01
      • 2012-07-18
      • 2012-12-29
      • 2017-04-26
      • 1970-01-01
      • 2010-11-04
      • 2011-02-13
      相关资源
      最近更新 更多