jQuery AJAX callback not fired
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.
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) {
}
});
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
});
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
}
});
精彩评论