关于jQuery超时错误处理

发布于 2024-10-08 17:00:06 字数 2000 浏览 0 评论 0原文

最近,我发现一些关于超时的错误。

请看下面的代码。

  • jQuery 超时:5000 毫秒
  • Servlet 睡眠:10000 毫秒

客户端 Javascript

    $.ajaxSetup({
    url: "http://localhost:8888/{mydomain}",
    timeout : 5000,
    error:function (xhr, textStatus, thrownError){
        switch(xhr.status) {
            case 0 :
                $('#msg').append("0");
                break;
            case 404:
                $('#msg').append("404 ERROR."); 
                break;
            case 500:
                $('#msg').append("500 ERROR.");
                break;
            case 408:
                $('#msg').append("408 ERROR.");
                break;              
            default :
                $('#msg').append(xhr.status);
                break;
        }
    }       
});

$(function() {
        $.ajax({
            type: "GET",
            dataType:"JSON",
            data:{send a some data},
            success:function(response){
                alert(response);
            }
        })      
});

Java 进程

@Path("/{mydomain}")
public class DummyResource {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String getMethod() throws Exception {

        try {
            Thread.sleep(10000);            
        } catch(InterruptedException ie) {
            throw new WebApplicationException(Response.Status.SERVICE_UNAVAILABLE);
        }
        return "hello!";
    }
}

但是,Safari 和 Firefox 之间的错误是不同的。

  1. Safari(超时错误:我认为,这个结果是正确的。)

    <前><代码>> GET //localhost:8888/{mydomain} 已中止
  2. Firefox(成功,但发生了一些错误)

    <前><代码>> GET //localhost:8888/{mydomain} 200 OK 10.01s 未捕获的异常:[异常...“组件返回失败代码:0x80040111(NS_ERROR_NOT_AVAILABLE)[nsIXMLHttpRequest.status]”nsresult:“0x80040111(NS_ERROR_NOT_AVAILABLE)”位置:“JS框架:://localhost:8888/{mydomain}/ajax .html :: 匿名 :: 第 20 行数据:否]

我的系统信息

MacOSX : 10.6.5
浏览器:Safari 5.0.3、火狐 3.6.8

Recently, I found some error about timeout.

Please see below code.

  • jQuery timeout : 5000 ms
  • Servlet sleep : 10000 ms

Client side Javascript

    $.ajaxSetup({
    url: "http://localhost:8888/{mydomain}",
    timeout : 5000,
    error:function (xhr, textStatus, thrownError){
        switch(xhr.status) {
            case 0 :
                $('#msg').append("0");
                break;
            case 404:
                $('#msg').append("404 ERROR."); 
                break;
            case 500:
                $('#msg').append("500 ERROR.");
                break;
            case 408:
                $('#msg').append("408 ERROR.");
                break;              
            default :
                $('#msg').append(xhr.status);
                break;
        }
    }       
});

$(function() {
        $.ajax({
            type: "GET",
            dataType:"JSON",
            data:{send a some data},
            success:function(response){
                alert(response);
            }
        })      
});

Java process

@Path("/{mydomain}")
public class DummyResource {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String getMethod() throws Exception {

        try {
            Thread.sleep(10000);            
        } catch(InterruptedException ie) {
            throw new WebApplicationException(Response.Status.SERVICE_UNAVAILABLE);
        }
        return "hello!";
    }
}

But, error is different between Safari and Firefox.

  1. Safari (timeout error: I think, this result is right.)

    > GET //localhost:8888/{mydomain} Aborted
    
  2. Firefox(success, but some error occured)

    > GET //localhost:8888/{mydomain} 200 OK 10.01s
    uncaught exception: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: //localhost:8888/{mydomain}/ajax.html :: anonymous :: line 20" data: no]
    

My System Info

MacOSX : 10.6.5
Browser: Safari 5.0.3, Firefox 3.6.8

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文