空气XmlHtt prequest超时如果远程服务器脱机? [英] Air XmlHttpRequest time out if remote server is offline?

查看:134
本文介绍了空气XmlHtt prequest超时如果远程服务器脱机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写通过XmlHtt prequest服务器通信的AIR应用程序。

I'm writing an AIR application that communicates with a server via XmlHttpRequest.

这是我遇到的问题是,如果服务器无法访问,我异步XmlHtt prequest似乎永远不会失败。我onreadystatechange的处理程序检测打开的状态,但没有别的。

The problem that I'm having is that if the server is unreachable, my asynchronous XmlHttpRequest never seems to fail. My onreadystatechange handler detects the OPENED state, but nothing else.

有没有一种方法,使XmlHtt prequest超时?

Is there a way to make the XmlHttpRequest time out?

我必须做一些愚蠢的喜欢使用的setTimeout()等待了一会儿,然后中止(),如果没有建立连接?

Do I have to do something silly like using setTimeout() to wait a while then abort() if the connection isn't established?

编辑: 找到<一href="http://stackoverflow.com/questions/377231/problem-xmlhtt$p$pquest-handle-server-connection-lost">this,但在我的测试,包装我xmlhtt prequest.send()在try / catch块或设置在xmlhtt prequest.timeout(或超时或超时)的值没有任何影响。

Found this, but in my testing, wrapping my xmlhttprequest.send() in a try/catch block or setting a value on xmlhttprequest.timeout (or TimeOut or timeOut) doesn't have any affect.

推荐答案

通过AIR,与XHR其他地方,你必须在JavaScript设置一个计时器来检测连接超时。

With AIR, as with XHR elsewhere, you have to set a timer in JavaScript to detect connection timeouts.

var xhReq = createXMLHttpRequest();
xhReq.open("get", "infiniteLoop.phtml", true); // Server stuck in a loop.

var requestTimer = setTimeout(function() {
   xhReq.abort();
   // Handle timeout situation, e.g. Retry or inform user.
}, MAXIMUM_WAITING_TIME);

xhReq.onreadystatechange = function() {
  if (xhReq.readyState != 4)  { return; }
  clearTimeout(requestTimer);
  if (xhReq.status != 200)  {
    // Handle error, e.g. Display error message on page
    return;
  }
  var serverResponse = xhReq.responseText;  
};

来源

这篇关于空气XmlHtt prequest超时如果远程服务器脱机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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