I using $.ajax method in jquery to implement an ajax call on a PHP file. I want to submit 2 parameters to the PHP file which will return response as XML. This is what I am trying to do:
$.ajax({
type: "GET",
url: 'server.php',
dataType: 'xml',
data: {
'param1' : '2',
'param2': '3'
},
success: function(data) {
alert('success');
/开发者_运维问答/ do something with xml returned...
},
error: function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(xhr.responseText);
}
});
In the PHP file I've simply done this for debugging:
echo '<?xml version="1.0"?>
<a>vala</a>
<b>valb</b>';
die;
This returns an error and I receive two alerts, one says "200" and the other shows the XML shown above. What am I doing wrong?
Thanks.
Your xml output is not vaild instead of
echo '<?xml version="1.0"?>
<a>vala</a>
<b>valb</b>';
die;
echo '<?xml version="1.0"?>
<root>
<a>vala</a>
<b>valb</b>
</root>';
die;
200 means everything is OK.
Try adding header('Content-type: text/xml');
to the top of server.php.
Try changing die;
to exit();
Edit: my bad that's not right
精彩评论