使用AJAX加载XML,但没有显示结果

Loading XML with AJAX but it is not showing results

本文关键字:显示 结果 AJAX 加载 XML 使用      更新时间:2023-09-26

我正在加载一个XML文件,然后根据它们的标记显示它的内容,但我的代码似乎不起作用。这是我的XML

<?xml version="1.0" encoding="UTF-8"?> 
<xml> 
<word> 
  <threeletter>RIP</threeletter> 
  <fourletter>PIER</fourletter>
  <fiveletter>SPIRE</fiveletter> 
  <sixletter>SPIDER</sixletter> 
 </word>
</xml>

这是我的Ajax代码

$(document).ready(function() {
    $.ajax({ 
    url: "dictionary.xml", 
    success: function( xml ) { 
        $(xml).find("sixletter").each(function(){ 
            $("ul").append( 
              "<li>" + $(this).text() + "</li>"); 
        }); 
    } 
});
})

它们都在同一个文件夹中。

您的代码适用于我,使用jQuery 1.7.1

也许您没有对xml文件的通用读取访问权限?将浏览器导航到xml文件,如果没有加载,则说明存在问题。

尝试添加数据类型:


$(document).ready(function() {
    $.ajax({ 
    url: "dictionary.xml", 
    dataType: "xml",
    success: function( xml ) { 
        $(xml).find("sixletter").each(function(){ 
            $("ul").append( 
              "<li>" + $(this).text() + "</li>"); 
        }); 
    } 
});
})

希望它能帮助