javascript responseXML 为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6973202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
responseXML is null
提问by kanmani
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 file
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>
Anyone has idea why xmlhttp.responseXML
is returning as null?
任何人都知道为什么xmlhttp.responseXML
返回为空?
回答by Maxim Krizhanovsky
Make sure you have header('Content-type: application/xml');
in your PHP script.
Additionally, check the responseText - maybe you've got error?
确保你header('Content-type: application/xml');
的 PHP 脚本中有。此外,检查 responseText - 也许你有错误?
回答by duri
Your HTTP request is asynchronous. xmlhttp.responseXML
won't have some value until xmlhttp.readyState
has the value of 4
.
您的 HTTP 请求是异步的。xmlhttp.responseXML
不会有一些价值,直到xmlhttp.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();
}
Additionaly, I don't think you need the setRequestHeader
line. XML MIME type is required for response, not for request. Also, please respect good coding practices (don't forget var
, DRY, etc.)
另外,我认为你不需要这setRequestHeader
条线。响应需要 XML MIME 类型,而不是请求。另外,请尊重良好的编码习惯(不要忘记var
,DRY 等)
回答by JonathanDavidArndt
Recently, I migrated from Apache to nginx and had this same problem. Everything worked perfectly when loaded as simple files or from the Apache server, but responseXML
was always null when running on nginx.
最近,我从Apache迁移到nginx,遇到了同样的问题。当作为简单文件加载或从 Apache 服务器加载时,一切都运行良好,但responseXML
在 nginx 上运行时始终为空。
My particular situation also involved an XSL stylesheet to transform the XML:
我的特殊情况还涉及一个 XSL 样式表来转换 XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="main-template-transformer.xsl"?>
The content type of the regular XML file was coming back just fine. However, what mattered was the content type of the XSL file. (This was discovered by checking responseText
which was not null, and contained the entire text of the XSL file. Checking HTTP headers on this file revealed that the content type had changed between Apache and nginx.)
常规 XML 文件的内容类型恢复正常。但是,重要的是 XSL 文件的内容类型。(这是通过检查responseText
哪个不为空来发现的,并且包含 XSL 文件的整个文本。检查该文件的 HTTP 标头显示内容类型在 Apache 和 nginx 之间发生了变化。)
Content type should be either text/xml
or application/xml
. The default in nginx 1.10.3 is application/octet-stream
, and this will cause responseXML
to be always null.
内容类型应为text/xml
或application/xml
。nginx 1.10.3 中的默认值是application/octet-stream
,这将导致responseXML
始终为空。
This can be fixed by adding the following line to the JavaScript file:
这可以通过向 JavaScript 文件添加以下行来解决:
xmlhttp.overrideMimeType('text/xml');
This can be fixed by adding the following line to the nginx server configuration in "conf/mime.types":
这可以通过将以下行添加到“conf/mime.types”中的 nginx 服务器配置来解决:
text/xml xsl;
回答by Nicolas
I've just testing and found the solution.
我刚刚测试并找到了解决方案。
I don't kwow why but when you send xml header the XMLHttpRequest is not capable to parse it.
我不知道为什么,但是当您发送 xml 标头时,XMLHttpRequest 无法解析它。
For using DOM responseXML properties of XMLHttpRequest you have to remove the xml header.
要使用 XMLHttpRequest 的 DOM responseXML 属性,您必须删除 xml 标头。
In your case the xml response will be
在您的情况下,xml 响应将是
<main>
<food>
<type>6</type>
<region>5676</region>
</food>
<food>
<type>6</type>
<region>5676</region>
</food>
</main>