"The data necessary to complete this operation is not yet available" message in IE7 after jQuery AJAX timeout
I've seen bits and pieces of this problem documented around, but no clear solution. I'm making an asynchronous ajax call, using jQuery's $.ajax() function. I have code in the ajax function's error method to handle ajax timeouts (see code below), and everything works fine in Firefox. But in IE, I get the following message: "The data necessary to complete this operation is not yet available."
Before I go any further, here's 开发者_StackOverflowmy code:
error: function(xhr, status) {
var res = xhr.responseText;
var err = null;
if (res && res.charAt(0) == '{')
err = $.secureEvalJSON(res);
if (!err) {
if (status == "timeout") {
err = new CallbackException("The request has timed out.");
}
else if (xhr.status != 200) {
err = new CallbackException(xhr.status + " " + xhr.statusText);
}
else {
err = new CallbackException("Unknown Error Response");
}
err.detail = res;
}
else
err = new CallbackException(err); // force error to common format
if (errorHandler)
errorHandler(err, _I, xhr);
}
}
So you can see, the first thing I do in an error situation, is check xhr.responseText
. In Firefox, this string is empty (""
), but in IE, it's "The data necessary to complete this operation is not yet available". Even referencing the responseText
value of xhr
in IE throws an error, and the user gets an error dialog.
I've read in a couple places that I should wait until xhr.readyState == 4
until I reference xhr.responseText
, but if I put an if (xhr.readyState == 4) {
block around the code in my error function, it never runs in an ajax timeout case. When I inspect the readyState
property, its value is 1
, which I think is "loading".
Is this normal for an ajax timeout? Will the ready state ever be 4? If not, how can I ever reference xhr.responseText without IE blowing up?
Thanks.
I have read that you need to change your code to check that typeof(xhr.responseText) != 'unknown') I haven't confirmed that it works yet because its hard to reproduce the problem.
精彩评论