1

I'm, trying to load som data from an xml file using jQuery and ajax but i don't anything back from the ajax request.

The 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>

The 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);
    }
});

But the console log is empty. What am I missing here ?

/Martin

5
  • 1
    You are missing an examination of the Net tab of your browser's developer tools, and an error handler. Commented Sep 26, 2013 at 10:58
  • 1
    Why are you passing an XMLDOM to parseXML (which expects a string)? Commented Sep 26, 2013 at 10:59
  • What am I looking for at the Net tab and I do not get any errors. parseXML was from an example I found on the web. Commented Sep 26, 2013 at 11:07
  • When you specify dataType it'll turn returned data as that specific type so you don't need to parse it. If you still have problem, take a look at xml to check whether it's been filled or not. Commented Sep 26, 2013 at 11:39
  • That makes sence, thank you. And the xml returns fine in the console. Then I just need to know how to check for the key="NewSite" so that I kan get the text from that? Commented Sep 26, 2013 at 12:01

1 Answer 1

1

you have an error in your code, $(this).attr("CreateSite") can be null so $(this).attr("CreateSite").text() throws an exception. Below is the corrected code:

 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");
        }
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Well $(this).text(); returns the text from all of the nodes but I need to get the one where key has a specific value like key="CreateSite" how do I achieve that?
you just need to change the test: if($(this).attr("key")=="xxx"){ createsite= $(this).text(); } xxx: the key value to search

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.