本文介绍了jQuery的阿贾克斯txtStat不返回"超时"但"错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Netbean 6.9.1
GlassFish的3.0.1
jQuery的1.6.2
 

我测试我的web应用程序的超时的情况。

我的超时的理解时,Ajax请求被发送的,如果花时间指定金额(如30秒),然后超时回调函数被触发。

我读的jQuery超时太多:

timeoutNumber

设置超时时间(毫秒)的请求。这将覆盖任何全局等待时间由$ .ajaxSetup()。超时时间开始于该点的$就调用时;如果其他几个请求正在进行,并且浏览器没有连接可用的,它有可能为一个请求超时它可以被发送之前。 jQuery中的1.4.x及以下,XMLHtt prequest对象将处于无效状态,如果请求超时;访问任何对象成员可能会引发异常。在Firefox 3.0+只,脚本和JSONP请求不能被超时取消;如果超时时间到达后,该脚本将仍然运行。

不过,虽然我测试超时,误差函数被调用,txtStat显示错误时,它应该是超时。这是我的测试步骤:

测试步骤:

  1. 启动GlassFish服务器
  2. 进入测试页面(foo.jsp)
  3. 停止GlassFish服务器
  4. 单击该按钮上foo.jsp发送Ajax请求。

预期的结果:

Ajax请求被发送,但无法达到服务器和错误回调函数被调用,它的参数:txtStat应该有值超时

实际结果:

错误回调函数被调用,它的参数:txtStat有值错误

因此​​,这是预期的行为,或者是我的超时的理解错了?

我的阿贾克斯code:

  $。阿贾克斯({
        网址:../resources/plan/list/some
        键入:搞定,
        数据类型:JSON,
        超时:30000,
        成功:功能(数据,txtStat,XHR){
            的console.log(成功)
        },
        错误:函数(XHR,txtStat,errThrown){
            如果(txtStat ===超时){
                的console.log(超时);
            }
            其他{// txtStat ===错误,中止,parseerror
                执行console.log(txtStat);
            }
        },
        完成:功能(XHR,txtStat){
            的console.log(完成);
        }
    });
 

解决方案

事实上,你的请求超时的认识是不完全正确。当请求需要超过一定的时间,但是当服务器关闭,你会发现,响应立即返回发生超时。尝试导航到你的web应用程序的网页,而它没有运行,只需在浏览器中。例如:

它立即结束,因为浏览器无法连的连接。

要导致超时状态,让您的服​​务器上运行,但请求一个网页,其中做类似的 - 服务器将接受请求,但不发送配置AJAX长时间才反应耗尽。

的是超时:当请求被接受,但是一个响应不及时发送

Netbean 6.9.1
GlassFish 3.0.1
jQuery 1.6.2

I'm testing my web app for timeout situation.

My understanding of "timeout" is when Ajax request is being sent and if it took specified amount of time (like 30 sec), then timeout callback function is triggered.

I read jQuery Timeout too:

timeoutNumber

Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.

But while I'm testing timeout, error function is called and txtStat is shown "error" when it should be "timeout". Here is my test step:

Test step:

  1. Start Glassfish server
  2. Go to the test page (foo.jsp)
  3. Stop Glassfish server
  4. click on the button on foo.jsp to send ajax request.

Expected result:

Ajax request is sent but unable to reach to the server and error callback function is called and its argument: txtStat should have value "timeout".

Actual result:

The error callback function is called and its argument: txtStat has value "error"

So is this expected behavior or is my understanding of timeout wrong?

My ajax code:

$.ajax({
        url: "../resources/plan/list/some",
        type: "get",
        dataType: "json",
        timeout: 30000,
        success: function(data, txtStat, xhr) {
            console.log("success")
        },
        error: function(xhr, txtStat, errThrown) {
            if(txtStat === "timeout") {
                console.log("timeout");
            }
            else { //txtStat === "error", "abort", "parseerror"
                console.log(txtStat);
            }
        },
        complete: function(xhr, txtStat) {
            console.log("completed");
        }
    });
解决方案

Indeed, your understanding of a request timeout is not quite right. A timeout occurs when the request takes more than a certain amount of time, but when the server is down, you'll find that the response returns immediately. Try navigating to a page of your web app while it is not running, just in a browser. For example:

It finishes immediately, because the browser can't even connect.

To cause a timeout condition, keep your server running, but request a page which does something like Thread.sleep(60000) – the server will accept the request, but not send a response before the configured ajax time runs out.

That is a timeout: when a request is accepted, but a response is not sent in time.

这篇关于jQuery的阿贾克斯txtStat不返回"超时"但"错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:22