我有一些带有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>');
})
}
});
}您可以在此处查看XML文件:http://lamanai.org/catmap/xml/photobank.xml
但是,Ajax不显示解析后的XML,并在Chrome的控制台中抛出这个错误:Uncaught Error: Invalid XML: [object Document]。我在错误的时间调用Ajax吗?现在它在文档就绪上,因为我需要在初始页面加载时解析初始的"Charlie“类别。
如果删除xml = $.parseXML(xml);行,则会加载初始解析的XML,但随后在尝试使用xmlParser函数时会出现Uncaught ReferenceError: xml is not defined错误。
有什么提示吗?这是我第一次接触Ajax。
HTML:http://lamanai.org/catmap/
JS:http://lamanai.org/catmap/js/cameramap.js
XML:http://lamanai.org/catmap/xml/photobank.xml
--
更新:xml = $.parseXML(xml);行已被删除,但现在我在重新使用该函数时遇到了问题。下面的代码会导致xml is not defined错误。
$('#btn_arnoldo').click(function(){
xmlParser(xml, "Arnoldo");
});我可以使用下面的Ajax调用,并让它正常工作,但是每次我想要切换XML的哪个部分时,真的有必要执行Ajax吗?我以为最初的Ajax给了我一个可以解析的对象?
$('#btn_arnoldo').click(function(){
$.ajax({
type: "GET",
url: "xml/photobank.xml",
dataType: "xml",
success: function(xml){
xmlParser(xml, "Arnoldo");
}
});
}); 发布于 2012-02-21 09:45:17
它看起来已经被解析了。您的对象似乎已经包含一个xml文档,而parseXML应该用于将xml字符串转换为XML文档。如果你想访问所有的图片标签,你应该能够使用jQuery来迭代所有的图片标签。
https://stackoverflow.com/questions/9370937
复制相似问题