Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 563
Christos Mitsis Avatar asked May 18 '11 23:05

Christos Mitsis


1 Answers

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);
        }
    });
}
like image 72
Satyam Avatar answered Oct 12 '22 21:10

Satyam