Christos Mitsis
Christos Mitsis

Reputation: 333

return xml in jquery ajax

My problem is that I want to return an xml file from server back to client and parsing it using ajax function of jquery. This is the code:

Client:

$("#submit").click(function(){          
    $.ajax({  
        type: "POST",  
        url: "search.php",  
        data: "whatever",
        dataType: "xml",
        async: false,
        success: function(xml){
            var data = $('doctor',xml).text();
            alert(data);
        }
    });
});

Server(php file),

header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="utf-8"?>';
echo "<tables>";
echo "<doctor>Someone</doctor>";
echo "</tables>";

I have a blank alert and I do not know why??


ok I found it. my php file was in this form

//some code
include("other.php");
//some other code

where the other.php file was the file I posted above. I cut/paste the header so the final php file would be

//some code
header('Content-type: text/xml');
include("other.php");
//some other code

and other.php

echo '<?xml version="1.0" encoding="utf-8"?>';
echo "<tables>";
echo "<doctor>Someone</doctor>";
echo "</tables>";

now it works perfect. Thanks for your quick replies!

Upvotes: 6

Views: 51423

Answers (4)

Satyam
Satyam

Reputation: 31

This is working fine

Post.php file

if($_GET['id']!=""){    
    $array = array('satyam'  => 'satyam',
                   'class'   => 'B.TECH',
                   'company' => 'Ranosys');
}   

$new ='<?xml version="1.0" encoding="iso-8859-1"?><data>';
foreach($array as $key => $values){ 
    $new .= "<$key>$values</$key>";
}
echo $new.'</data>';

=================

function load_data(){
    $.ajax({
        url: "post.php",
        async: false, // stop browser for another activity
        data: "id=satyam",
        // dataType :'xml',
        error: function(e, b, error) { 
            for(var i in e){
              // alert(i);
            }
            alert(e.respone);
        },
        success: function(msg) {
            //alert($response);
            var data = $(msg).find("satyam").text();
            alert(data);
        }
    });
}

Upvotes: 3

Avitus
Avitus

Reputation: 15958

You have to change your function to be:

$("#submit").click(function(){       
    $.ajax({  
        type: "POST",  
        url: "search.php",  
        data: "whatever",
        dataType: "xml",
        async: false,
        success: function(xml){

            var xmlDoc;

            if (window.DOMParser) {
                parser = new DOMParser();
                xmlDoc = parser.parseFromString(xml, "text/xml");
            }
            else {// Internet Explorer
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = "false";
                xmlDoc.loadXML(xml);
            }
            var $response = $(xmlDoc);
            var data = $response.find("doctor").text()

            alert(data);
        }
    });
});

The reason for the if (window.DOMParser) { is that you'll have an issue with IE doing the parsing.

Upvotes: 0

David Rodrigues
David Rodrigues

Reputation: 12532

You need to parse this XML (I really don't understand why , but...), you can do it by do:

$(xml).find('doctor').text();

Bye. :)

Upvotes: 0

Kevin Ennis
Kevin Ennis

Reputation: 14456

Try this: var data = $(xml).find('doctor').text()

In your example, 'xml' is not a jQuery object.

Upvotes: 1

Related Questions