AJAX错误:完成此操作所需的数据不可用 [英] AJAX Error: The data necessary to complete this operation is unavailable
问题描述
每当我向AJAX请求发送一个返回一个类型为text / plain的小响应的处理程序时,我会在IE中收到这个错误。似乎这个错误会开始发生,发生几次,然后它会停止。非常讨厌。
Every once and a while I get this error in IE when making an AJAX request to a handler that returns a small response of type text/plain. It seems that this error will start happening, occur a few times, and then it will stop. Very annoying.
我正在使用最新的jQuery库。当我尝试访问xhr.responseText时,该错误会抛出在complete()函数中。如何防止这种情况发生?
I am using the latest jQuery library. The error throws in the complete() function when I try to access xhr.responseText. How can I prevent this from happening?
$.ajax({
url: "Inquire.ashx",
data: data,
dataType: "text",
timeout: 5000,
complete: function(xhr, status) {
var resp = xhr.responseText; // ERROR!
if(resp.substr(0, 4) == "http")
window.open(resp, "PopWin");
else
showError(resp);
}
});
推荐答案
事实证明,错误是因为XmlHttpRequest的 readyState 属性为3,这意味着请求仍在进行中。我不明白为什么jQuery在readyState为4之前触发我的完整功能!我把它放在我成功回调的最顶端,并且没有看到错误,因为...
As it turns out, the error was caused because the readyState property of XmlHttpRequest was at 3, which means the request is still in process. I don't understand why jQuery fires my complete function before readyState is 4! I put this at the very top of my success callback, and have not seen the error since...
if(xhr && xhr.readyState != 4) {
setTimeout(arguments.callee, 50);
return;
}
这是一个非常丑的解决方案,我讨厌,但似乎修复问题。
This is a very ugly solution that I hate, but it seems to fix the problem.
这篇关于AJAX错误:完成此操作所需的数据不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!