Javascript responseXML 始终为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3781387/
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 always null
提问by WoF_Angel
Im using firefox 3.6.10, and firebug to debug
我使用 firefox 3.6.10 和 firebug 进行调试
So, here is my code:
所以,这是我的代码:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url,false);
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.send(null);
alert(xmlhttp.responseXML);
responseXMLis always null, and i've tried it on several URLs from different domains. I have also tried it asynchronously, it's the same result. The responseTextis always properly returned, no problems with it.
responseXML始终为空,我已经在来自不同域的多个 URL 上尝试过。我也异步尝试过,结果是一样的。该responseText的总是正确返回,与它没有任何问题。
My goal is to get the responseXML.documentElement.
我的目标是获得responseXML.documentElement。
Thanks for your help.
谢谢你的帮助。
EDIT-----------
This javascript code was executed from a Greasemonkey userscript, i made surte its the same origin as the requested url. Also i tried executing from firebug console, again ensuring the origin policy. Same error on both.
Gotta hate javascript.
编辑-----------
此javascript 代码是从Greasemonkey 用户脚本执行的,我将surte 设为与请求的url 相同的来源。我还尝试从萤火虫控制台执行,再次确保原始策略。两者相同的错误。
得讨厌 javascript。
采纳答案by hvgotcodes
I bet you are violating the same origin policy.
我敢打赌你违反了同源政策。
For XHRs, you must have the same protocol, domain, port, etc. So if you are running an app on localhost:8080/app, you CANNOT ajax to www.cnn.com.
对于 XHR,您必须具有相同的协议、域、端口等。因此,如果您在 localhost:8080/app 上运行应用程序,则不能 ajax 到 www.cnn.com。
Different browsers handle this differently; I have seen FF do what you describe, which is the request appears to return normally but there is no data...
不同的浏览器对此有不同的处理方式;我已经看到 FF 做了你所描述的,即请求似乎正常返回但没有数据......
回答by MooGoo
Besides the cross-domain issues already mentioned, responseXML
requires completely valid XML and probably the correct Content-Type
in the response headers sent from the server. It is very unlikely that either of these requirements would be met by the average website.
除了已经提到的跨域问题,还responseXML
需要完全有效的 XML 并且可能在Content-Type
从服务器发送的响应头中是正确的。普通网站不太可能满足这些要求中的任何一个。
For the latter issue, you can use
对于后一个问题,您可以使用
xmlhttp.overrideMimeType('application/xml');
before you send the request to force the response to be interperted as XML. Still if the response is not valid XML, you will only get null
.
在您发送请求以强制将响应解释为 XML 之前。如果响应不是有效的 XML,您将只会得到null
.
回答by Kenny
If i recall correctly , this is a known problem with firefox ( i have had the same problem before ).
如果我没记错的话,这是 Firefox 的一个已知问题(我以前也遇到过同样的问题)。
The fix is to parse the responseText back to an XML document , and then use this.
解决方法是将 responseText 解析回 XML 文档,然后使用它。
Something like this :
像这样的事情:
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml");
回答by Mic
Try to open the value of url
directly in the browser. You should get some error information.
If you see a parsing error, chances are your encoding is wrong and you have a special character in your XML that makes it invalid.
尝试url
直接在浏览器中打开的值。你应该得到一些错误信息。
如果您看到解析错误,很可能是您的编码错误,并且您的 XML 中有一个特殊字符使其无效。
To avoid that, you need to be sure that all the chain is properly encoded.
为避免这种情况,您需要确保所有链都正确编码。
If it is a static XML file, you need to set correctly your editor encoding when saving it. The encoding that does it all(almost) is UTF-8, it is usually a property you can choose in your editor settings or in the save dialog.
如果是静态 XML 文件,则需要在保存时正确设置编辑器编码。完成这一切(几乎)的编码是 UTF-8,它通常是您可以在编辑器设置或保存对话框中选择的属性。
If it is dynamically generated. Your data, the page and the server response must be properly encoded too. And your XML starting with <?xml version="1.0" encoding="UTF-8"?>
如果是动态生成的。您的数据、页面和服务器响应也必须正确编码。而你的 XML 开头<?xml version="1.0" encoding="UTF-8"?>
You can try first with a very basic and static XML:
您可以先尝试使用非常基本的静态 XML:
<?xml version="1.0" encoding="UTF-8"?><root>hi</root>
And then add the steps, one by one to make it like yours, without breaking it.
然后一步一步添加步骤,使其像您的一样,而不会破坏它。
回答by CENTRI
Was fooling around with this for hours and finally figured out the stupid little error that was messing me up...
在这个问题上鬼混了几个小时,终于找出了让我搞砸的愚蠢的小错误......
If you are like me, you like to keep your JavaScript code in an external ".js" file.
如果您像我一样,喜欢将 JavaScript 代码保存在外部“.js”文件中。
Therefor, using xmlhttp.open("GET","yourxmlfile.xml",false) will always search for the XML file RELATIVE to the HTML document, even if the code is in an external JavaScript file.
因此,使用 xmlhttp.open("GET","yourxmlfile.xml",false) 将始终搜索与 HTML 文档相关的 XML 文件,即使代码位于外部 JavaScript 文件中。
If the responseText returns null, the local file could not be found in the specified path location. And if async is set to true, the file will be created and the response text will carry the contents of an empty XML document.
如果 responseText 返回 null,则在指定的路径位置找不到本地文件。如果 async 设置为 true,则将创建该文件,并且响应文本将携带空 XML 文档的内容。
Example:
例子:
xmlDoc = xmlhttp.responseText; //String data type
xmlDoc = xmlhttp.responseText; //字符串数据类型
or
xmlDoc = xmlhttp.responseXML; //XML data type
xmlDoc = xmlhttp.responseXML; //XML数据类型
- folder1 = folder2, index.html.
- folder2 = index.js, yourxmlfile.xml.
- path to XML from HTML = "folder2/yourxmlfile.xml", not "yourxmlfile.xml".
- 文件夹 1 = 文件夹 2,index.html。
- folder2 = index.js, yourxmlfile.xml。
- 从 HTML 到 XML 的路径 =“folder2/yourxmlfile.xml”,而不是“yourxmlfile.xml”。
And remember to parse the XML doc into "text/xml", after opening and before sending request.
并记得在打开之后和发送请求之前将 XML 文档解析为“text/xml”。
Example:
例子:
- xhttp.overrideMimeType('text/xml');
- Chrome: xmlDoc = (new DOMParser()).parseFromString(xmlDoc,"text/xml");
- xhttp.overrideMimeType('text/xml');
- Chrome: xmlDoc = (new DOMParser()).parseFromString(xmlDoc,"text/xml");
回答by CENTRI
For me it was a simple problem. There was a syntax error in my xml/php file. When I viewed the file in my browser, the browser did not detect any errors.
对我来说,这是一个简单的问题。我的 xml/php 文件中有语法错误。当我在浏览器中查看文件时,浏览器没有检测到任何错误。
Make sure the elements within your XML document are properly closed!
确保 XML 文档中的元素已正确关闭!
回答by Engineer
Malformed XML of any sortwill cause this problem. For example, I had an attribute renderer
(without ="something"
) which is invalid and causes DOMParser
- which is called by XMLHTTPRequest
- to choke. Hence load
event will contain the text response but not the XML.
任何形式的畸形 XML都会导致此问题。例如,我有一个无效的属性renderer
(没有="something"
)并导致DOMParser
- 被调用XMLHTTPRequest
- 窒息。因此load
事件将包含文本响应但不包含 XML。
回答by Gunnar Bernstein
I had a hard time to find a correct xml example. If you get null try an XML Validator. For me the <root>
element was missing.
我很难找到正确的 xml 示例。如果您得到空值,请尝试使用 XML 验证器。对我来说,<root>
缺少元素。
<root>
<info><p>Array
(
)
</p>
</info>
<itemData>{"id":"40","client_id":"1","nameUnique":"Lore ipsum","description":null,"userComment":null,"last_modified":"2018-12-15 02:48:57"} </itemData>
</root>