开发者

JQuery $.ajax and xml

开发者 https://www.devze.com 2023-03-15 18:11 出处:网络
I have this code: var xml = $.ajax({url: \"aardvark-ajax.php?rand=\"+Math.random(),async: false, dataType:\'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());
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消