tags:

views:

80

answers:

3

I have an AJAX request:

$.ajax({
   url : "proxy.php",
   type : "POST",
   data : xmlData,
   contentType : "application/x-www-form-urlencoded",
   processData : false,
   success : function(data) {
       // success
   },
   error : function(data) {
       // error
   },    
});

Which is being answered by a PHP proxy:

header('Content-type: text/xml');
echo $someXmlResponse;
exit();

None of the callbacks are getting fired, neither the success nor the error.

This isn't the first time I've got this. What's going on?


Edit: some updates - trailing comma was not the issue, but thanks for pointing it out. Console shows no errors. Firebug shows request is sent and received properly. Request returns with status 200 OK, data returns correctly.


Thanks for all the help guys. All your feedback was in place. However none actually solved the issue. It looks like a bug in Firefox 4b5.

+2  A: 

This may not be your issue, but is something that will cause problems in some versions of IE. You have a trailing comma after the error: callback.

Whether or not it is the issue, you should remove that.

$.ajax({
   url : "proxy.php",
   type : "POST",
   data : xmlData,
   contentType : "application/x-www-form-urlencoded",
   processData : false,
   success : function(data) {
       // success
   },
   error : function(data) {
       // error
   }  // <--- removed trailing comma
});
patrick dw
There you go, Eagle eye! :P
Garis Suero
Thanks, but that's not the issue.
Yuval A
+2  A: 

The behavior you are observing could happen if the server returns invalid XML that cannot be parsed. Try returning something that is guaranteed to be valid XML:

header('Content-Type: text/xml'); // <-- Notice the Content-Type header casing
echo '<foo/>';
exit();

Also you are setting the contentType to application/x-www-form-urlencoded, while your data parameter is called xmlData assuming XML. By setting the processData parameter to false you are indicating that the data should be posted as is and not automatically converted to application/x-www-form-urlencoded and still you indicate this content type in the request which seems contradictory.

Also if you expect XML from the server you could specify dataType: 'xml'.

So your request might look like this:

$.ajax({
    url: 'proxy.php',
    type: 'POST',
    contentType: 'text/xml',
    data: '<request/>',
    processData: false,
    dataType: 'xml',
    success: function(data) {

    },
    error: function(data) {

    }
});
Darin Dimitrov
Darin, what exactly does the [data:'<request/>'] do here? Before, he was using that to send a value that I assume was held by a variable named 'xmlData' which you have replaced with '<request />'. What exactly happens here?
d2burke
A: 

have you tried using 'dataType' instead of 'data'? a la:

Try:

$.ajax({
   url : "proxy.php",
   type : "POST",
   dataType : 'xml',
   contentType : "application/x-www-form-urlencoded",
   processData : false,
   success : function(xml) {
       // success
   },
   error : function(xml) {
       // error
   }  
});
d2burke
No - obviously I have to post the xml data.
Yuval A
well, my apologies, you don't always need to send to receive and I wasn't clear on that since your php does not explicitly state that it's receiving anything, but still an oversight on my part. Anyway, then would you still not need to declare the dataType? Have you tried this?
d2burke
Tried that - no good.
Yuval A