jQuery.ajax解析XML

时间:2011-03-13 04:19:32

标签: xml jquery

我正在尝试使用jQuery的ajax函数获取xml文件的内容。


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

facts.xml很简单:


<?xml version="1.0" encoding="utf-8"?>
<axiom>
  <sentence>
      <part>something</part>
  </sentence>
</axiom>

当我在firefox中运行它时,alert会给我“({})”。我一直试图找出我做错的地方,但我无法理解。谁能给我一些帮助?

非常感谢!

2 个答案:

答案 0 :(得分:4)

toSource应该为您提供相应对象的JavaScript源代码,但它不能也不适用于任何对象。尝试向DOM对象询问其他内容,例如.documentElement.tagName

答案 1 :(得分:3)

我想你可能想要这样的东西。

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