I am trying to get XML response in javascript array like:
<script type="text/javascript">
var arrayName = [];
var arrayValue [];
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "data.xml",
dataType: "xml",
success: function(xml) { parseXml(xml); }
});
});
function parseXml(xml)
{
$(xml).find("category").each(function()
{
arrayName.push($(this).find("name").text());
arrayValue.push($(this).find("value").text());
});
}
//Do some javascript stuff using arrayName and arrayValue for example:
alert(arrayName[0]);
alert(arrayValue[0]);
</script>
But I am not able to get xml response in javascript array.
my XML file is :
<?xml version="1.0" encoding="utf-8" ?>
<data>
<category id="category1">
<name>Error</name>
<value>41</value>
</category>
<category id="category2">
<name>Warning</name>
<value>23</value>
</category>
<category id="category3">
<name>Info</name>
<value>46</value>
</category>
</data>
please help me to solve this problem
alert
s in the callback. They are executed before the response comes from the server. Ajax is asynchronous. – Felix Kling May 28 '11 at 14:16