jQuery - 尝试解析 XML 时,未捕获的错误:无效的 XML:[对象文档]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9370937/
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
jQuery - When trying to parse XML, Uncaught Error: Invalid XML: [object Document]
提问by petroica
I have some jQuery with an Ajax call that looks like this:
我有一些带有 Ajax 调用的 jQuery,如下所示:
$.ajax({
type: "GET",
url: "xml/photobank.xml",
dataType: "xml",
success: function(xml){
xmlParser(xml, "Charlie");
}
});
function xmlParser(xml, landOwner) {
//Initial photos do not load if following line is used.
//xml = $.parseXML(xml);
$('#photo_container').empty();
console.log('1');
$(xml).find('Landowner').each(function(){
console.log('2');
var landownerName = $(this).attr('Name');
if (landownerName === landOwner) {
$(this).find('Photo').each(function(){
$("#photo_container").append('<div class="photo ' + $(this).find("Category").text().toLowerCase() + '"><p>' + $(this).find("Animal").text() + '</p>'+ '<img class="photobank" src="images/' + $(this).find("Image").text() + '" />' + '</div>');
})
}
});
}
You can see the XML file here: http://lamanai.org/catmap/xml/photobank.xml
您可以在此处查看 XML 文件:http: //lamanai.org/catmap/xml/photobank.xml
However, the Ajax does not display the parsed XML, and throws this error in Chrome's console: Uncaught Error: Invalid XML: [object Document]
. Am I calling the Ajax at the wrong time? Right now it is on document ready, since I need the initial "Charlie" category parsed on the initial page load.
但是,Ajax 不显示解析的 XML,并在 Chrome 的控制台中抛出此错误:Uncaught Error: Invalid XML: [object Document]
. 我是不是在错误的时间调用了 Ajax?现在它已准备好文档,因为我需要在初始页面加载时解析初始“查理”类别。
If I remove the xml = $.parseXML(xml);
line, the initial parsed XML does load, but then I get Uncaught ReferenceError: xml is not defined
errors when trying to use the xmlParser
function afterwards.
如果删除该xml = $.parseXML(xml);
行,则会加载初始解析的 XML,但随后Uncaught ReferenceError: xml is not defined
在尝试使用该xmlParser
函数时会出错。
Any hints? This is my first encounter with Ajax.
任何提示?这是我第一次接触 Ajax。
HTML: http://lamanai.org/catmap/
HTML:http: //lamanai.org/catmap/
JS: http://lamanai.org/catmap/js/cameramap.js
JS:http: //lamanai.org/catmap/js/cameramap.js
XML: http://lamanai.org/catmap/xml/photobank.xml
XML:http: //lamanai.org/catmap/xml/photobank.xml
--
——
Update: The xml = $.parseXML(xml);
line was removed, but now I'm having trouble re-using the function. The following code results in xml is not defined
errors.
更新:该xml = $.parseXML(xml);
行已删除,但现在我无法重新使用该功能。以下代码导致xml is not defined
错误。
$('#btn_arnoldo').click(function(){
xmlParser(xml, "Arnoldo");
});
I can use the following Ajax call instead, and have it work, but is it really necessary to do the Ajax each time I want to switch what part of the XML I want to use? I thought the initial Ajax gives me an object that I can parse from?
我可以改用下面的 Ajax 调用,让它工作,但是每次我想切换我想使用的 XML 的哪一部分时,真的有必要执行 Ajax 吗?我认为最初的 Ajax 给了我一个我可以解析的对象?
$('#btn_arnoldo').click(function(){
$.ajax({
type: "GET",
url: "xml/photobank.xml",
dataType: "xml",
success: function(xml){
xmlParser(xml, "Arnoldo");
}
});
});
回答by Edward
It looks like it is already parsed. Your xml object seems to contain an xml document already, while parseXMLis supposed to be used to convert an XML string to an XML document. If you want to access all the Image tags, you should be able to use jQuery to iterate all of them using each.
看起来它已经被解析了。您的 xml 对象似乎已经包含一个 xml 文档,而parseXML应该用于将 XML 字符串转换为 XML 文档。如果你想访问所有的 Image 标签,你应该能够使用 jQuery 来迭代所有的标签。