JQuery $.ajax and xml
I have this code:
var xml = $.ajax({url: "aardvark-ajax.php?rand="+Math.random(),async: false, dataType:'xml'});
alert(xml);
jQuery('image', xml).each(function(i)
{
alert(jQuery(this).text());
});
However, the each loop isn't working, so I'm guessing my $.ajax query is wrong.
This is the code in aardvark-ajax.php:
$arrImages = array(1,2,3,4);
header('Content-Type: text/xml');
开发者_如何学JAVAecho '<data>';
echo '<total>' . $intTotal . '</total>';
foreach ($arrImages as $strImage)
{
echo ' <image>' . $strImage . '</image>';
}
echo '</data>';
Thge first alert(xml) outputs [object XMLHttpRequest].
Any ideas?
you need to access the content of the XMLHttpRequest, which is easier done in the success handler of your ajax request. Something like this might work:
$.ajax({
url: "aardvark-ajax.php?rand="+Math.random(),
async: false,
dataType:'xml'
success:function(result) {
jQuery('image', result).each(function(i)
{
alert(jQuery(this).text());
});
}
});
Try:
jQuery('image', xml.responseText).each(function(i){
alert(jQuery(this).text());
});
精彩评论