jQuery ajax txtStat does not return “timeout” but “error”

☆樱花仙子☆ 提交于 2019-12-13 17:32:44

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/9713471/jquery-ajax-txtstat-does-not-return-timeout-but-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!