JQuery.ajax parse XML

I am trying to get the contents of an xml file using jQuery 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 tried to determine where I was doing wrong, but I could not understand. Can anyone help me out?

Thank you so much!

+3
source share
2 answers

toSourceshould provide you with the equivalent JavaScript source for the object in question, but it cannot and does not work for any object. Try asking the DOM object for something else, for example .documentElement.tagName.

+4
source

I think you might need something similar.

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

All Articles