return xml in jquery ajax
49,130
Solution 1
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);
}
});
}
Solution 2
Try this: var data = $(xml).find('doctor').text()
In your example, 'xml' is not a jQuery object.
Author by
Christos Mitsis
Updated on July 09, 2022Comments
-
Christos Mitsis almost 2 years
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!
-
Avitus about 13 yearsThis won't work in IE per stackoverflow.com/questions/562283/…
-
Christos Mitsis about 13 yearsnot working at all. i run the application on firefox 4. i tried IE9 but it doesn't show the alert at all.