jQuery没有读取xml文件

时间:2010-09-09 04:56:30

标签: jquery xml file

我有一个xml文件xyz.xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<legends>
<legend>
<number>1</number>
<contentString>abcd</contentString>
</legend>
<legend>
<number>2</number>
<contentString>efg</contentString>
</legend>
<legend>
<number>3</number>
<contentString>hij</contentString>
</legend>
</legends>

我试图用jQuery阅读:

$(document).ready(function() {       

   $.get("xyz.xml",{},function(xml){

   var randomnumber=Math.floor(Math.random()*3); 


   $('legend',xml).each(function() {         

            if(randomnumber == $(this).find("number").text())
            {
          var c = "contentString";

          var legendStr = $(this).find(c).text();

          alert(legendStr);
            }               

   });
   });

 }); 

jQuery代码没有进入函数$('legend',xml).each(function()。

为什么会这样。

2 个答案:

答案 0 :(得分:2)

如果服务器没有返回jQuery可以映射到XML的MIME类型,那么它将对响应的类型做出错误的猜测。指定数据类型以防止猜测:

$.get("xyz.xml",{},function(xml){ ... },"xml");

答案 1 :(得分:1)

你有没试过这个?

$(xml).find("legend").each(function() {
   ...
});