1

我正在尝试使用 jQuery 和 ajax 从 xml 文件加载 som 数据,但我没有从 ajax 请求返回任何内容。

xml

<?xml version="1.0" encoding="utf-8" ?>
<items>
  <item key="NewSite">Create new site</item>
  <item key="EditSite">Edit site</item>
  <item key="DeleteSite">Delete site</item>
  <item key="ViewSite">View site</item>
  <item key="ViewSiteList">View full site list</item>
</items>

jQuery

var createsite = "";

//--- get trasnlated text for the notify box
$.ajax({
    url: "/Areas/Admin/Content/Scripts/admin/da-DK.xml",
    method: "GET",
    dataType: "xml",
    success: function (xml) {
        var xmlDoc = $.parseXML(xml),
        $xml = $(xmlDoc);

        $xml.find('item').each(function () {
            createsite = $(this).attr("CreateSite").text();
        });

        console.log(createsite);
    }
});

但是控制台日志是空的。我在这里想念什么?

/马丁

4

1 回答 1

1

您的代码中有错误,$(this).attr("CreateSite")可以为空,因此$(this).attr("CreateSite").text()会引发异常。以下是更正后的代码:

 var createsite = "";

    //--- get trasnlated text for the notify box
    $.ajax({
        url: "/Areas/Admin/Content/Scripts/admin/da-DK.xml",
        method: "GET",
        dataType: "xml",
        success: function (xml) {
            var xmlDoc = $.parseXML(xml),
            $xml = $(xmlDoc);

            $xml.find('item').each(function () {
               if($(this).attr("CreateSite")){
                  createsite=  $(this).text();
                }
            });

            console.log("createsite=>"+createsite);
        },
        error: function () {
            console.log("call failled");
        }
    });
于 2013-09-26T11:46:07.330 回答