7

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!

1
  • Does $(xml).find("doctor") work? Commented May 18, 2011 at 23:20

4 Answers 4

3

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);
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

1 Comment

This won't work in IE per stackoverflow.com/questions/562283/…
0

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

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

Bye. :)

Comments

0

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.

1 Comment

not working at all. i run the application on firefox 4. i tried IE9 but it doesn't show the alert at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.