Mark Blades
Mark Blades

Reputation: 397

Sending ajax request to PHP as GET, but parsing response as XML

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.

Upvotes: 0

Views: 332

Answers (3)

Jaime
Jaime

Reputation: 1410

200 means everything is OK.

Try adding header('Content-type: text/xml'); to the top of server.php.

Upvotes: 1

confucius
confucius

Reputation: 13327

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;

Upvotes: 2

Clive
Clive

Reputation: 36956

Try changing die; to exit();

Edit: my bad that's not right

Upvotes: 0

Related Questions