AJAX错误:要完成此操作的数据是不可用 [英] AJAX Error: The data necessary to complete this operation is unavailable

查看:111
本文介绍了AJAX错误:要完成此操作的数据是不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每一次,一段时间,我做一个AJAX请求处理程序返回类型text / plain的一个小的反响时出现在IE这个错误。看来,这个错误会开始发生,发生了几下,然后它会停下来。非常讨厌。

我使用的是最新的jQuery库。在完整的()函数的错误抛出,当我尝试访问xhr.responseText。我怎样才能prevent这种情况的发生?

  $。阿贾克斯({
    网址:Inquire.ashx
    数据:数据,
    数据类型:文本,
    超时:5000,
    完成:功能(XHR,状态){
      VAR RESP = xhr.responseText; // 错误!

      如果(resp.substr(0,4)==的http)
        的window.open(RESP,PopWin);
      其他
        showError(RESP);
    }
  });
 

解决方案

事实证明,该错误造成的,因为在的 readyState的财产是在3,这意味着该请求仍在处理中。我不明白为什么jQuery的触发我的完整功能之前,readyState为4!我把这个在我的成功回调的最顶端,而没有看到错误,因为...

 如果(XHR&安培;&安培;!xhr.readyState = 4){
    的setTimeout(arguments.callee的,50);
    返回;
  }
 

这是我最讨厌一个很丑陋的解决方案,但它似乎解决问题。

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.

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);
    }
  });

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆