jQuery.ajax parse XML
I'm trying to get the contents of an xml file using jQuery's ajax function.
$(docu开发者_Python百科ment).ready(function(){
$.ajax({
url: 'facts.xml',
dataType: 'xml',
success: parseXML
});
function parseXML(xml){
alert(xml.toSource());
//...
}
}
facts.xml is simple:
<?xml version="1.0" encoding="utf-8"?>
<axiom>
<sentence>
<part>something</part>
</sentence>
</axiom>
When I run it in firefox, alert gives me "({})". I've been trying to identify where I was doing wrong, but I couldn't figure it out. Can anyone give me some help?
Thanks a lot!
toSource
is supposed to give you the equivalent of the JavaScript source for the object in question, but it can't and doesn't work for just any object. Try asking the DOM object for something else, such as .documentElement.tagName
instead.
I think you might want something like this.
$(document).ready(function(){
$.ajax({
url: 'facts.xml',
dataType: 'xml',
success: function(responseXML) {
alert($(responseXML).text());
}
});
}
精彩评论