0

I have a XML document:

<?xml version='1.0' ?>
<Root>
<Row>
<title>Homeward Bound</title>
</Row>
<Row>
<title>Escape to Witch Mountain</title>
</Row>
....
....
</Root>

My jQuery to read the XML is:

$(document).ready(function () {

   var data;
$.ajax({
    type: "GET",
    url: "titles.xml",
    dataType: "xml",
    success: function (e) {
        data = e;
    },
    error: function () {
        alert("The XML File could not be processed correctly.");
    }
});
alert($(data).find("Root Row").length);

... .. .

The alert shows 0 - despite there being several records.

Is there something wrong with the way I'm reading the XML file - or is it my ".find" that is wrong?

Thanks for any help, Mark

Mark
  • 7,778
  • 24
  • 89
  • 147

1 Answers1

1

scope of success argument remains in method only. set value variable to variable and then use the variable outside. or write the code in success function:

$(document).ready(function () {
 $.ajax({
 type: "GET",
 url: "titles.xml",
 dataType: "xml",
 success: function (e) {
    alert($(data).find("Root Row").length);
 },
 error: function() {
    alert("The XML File could not be processed correctly.");
 }});
 });
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125