3

I'm trying to get the contents of an xml file using jQuery's ajax function.


$(document).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!

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick response! I tried something like $(xml).find('part').text() then and it works fine ;)
3

I think you might want something like this.

$(document).ready(function(){
    $.ajax({
        url: 'facts.xml',
        dataType: 'xml',
        success: function(responseXML) {
            alert($(responseXML).text());
        }
    });
}  

2 Comments

I think alert(responseXML) just gives "[object XMLDocument]", but doesn't really show the contents of the xml file.
.html() doesn't work with XML, see api.jquery.com/html, .text() would be fine, though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.