jQuery AJAX success handler not functioning as expected
I'm experiencing a very strange problem with jQuery and IE. I'm using jQuery 1.4.4 with IE8. I try an AJAX call with:
$.ajax({
url: '/events/do_something',
dataType: 'json',
开发者_如何转开发 data : params,
beforeSend : function() {
setPageStateLoading(true);
},
success : function(data){
doSomethingElse(data, false);
},
error : function(XMLHttpRequest) {
openErrorDialog(XMLHttpRequest.responseText);
},
complete : function(XMLHttpRequest, textStatus) {
setPageStateLoading(false);
}
});
Although the request is successful the error handler gets called because of a parse error. There's nothing wrong with the returned JSON (it works perfectly on Firefox), the problem is line 6229 in jQuery:
var ct = xhr.getResponseHeader("content-type") || "",
Which fails with "Object doesn't support this property or method" and causes a parse error.
Right now I'm using the complete handler with an if statement as a workaround but this isn't really a very good idea. Any ideas on how to fix this properly?
Have you tried leaving dataType unset and then using jQuery's .parseJSON() (http://api.jquery.com/jQuery.parseJSON/)? This way you'll be able to see if there's an error in the response.
精彩评论