JQuery超时问题

发布于 2024-07-12 06:08:17 字数 754 浏览 2 评论 0原文

我发现使用 JQuery 超时存在问题,我有一个带有脚本的页面,它设置了超时并等待服务器的响应。 问题是当超时必须执行时,简而言之,它“不执行任何操作”。 我认为该脚本不会中断与服务器的连接并等待服务器响应,因为在服务器端,连接持续 10 秒(但超时设置为 5 秒)。 当服务器在与客户端不同的域名中工作时,我会看到此问题,而当服务器和客户端在本地工作时,则不会出现此问题。

您是否知道发生此错误的原因或如何关闭脚本连接?

// client 

$.ajax({

    type: "GET",

    url: "some.php",

    data: "name=John&location=Boston",

    timeout: 5000,

    success: function(msg){

        alert( "Data Saved: " + msg );

    },
    error: function(request, errorType, errorThrown){

        alert("opppsssss .... ");
    }
});

和服务器代码:

//some.php

< ?

//simulate long task

sleep(10); //sleep 10 seconds

//send response

echo "some test data";

? >

最好的问候

多梅尼科

I have found a problem with use JQuery timeout, i have a page with script, it's set a timeout and waiting response from server. The problem is when timeout have to perform, in short words, it "no action". I think that script not interrupt the connection with server and wait for server response, because in server side the connection is on for 10 sec (but timeout is set 5 sec).
I see this problem when server work in different domain name then client, whereas when the server and client work in local this problem there isn't.

Have you a idea because this error happened or how to close the Script connection?

// client 

$.ajax({

    type: "GET",

    url: "some.php",

    data: "name=John&location=Boston",

    timeout: 5000,

    success: function(msg){

        alert( "Data Saved: " + msg );

    },
    error: function(request, errorType, errorThrown){

        alert("opppsssss .... ");
    }
});

and the server code:

//some.php

< ?

//simulate long task

sleep(10); //sleep 10 seconds

//send response

echo "some test data";

? >

Best Regards

Domenico

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2024-07-19 06:08:17

从表面上看,你是从后到前做的。 您已将超时设置为 5000 毫秒(5 秒),因此如果服务器端代码的响应时间超过 5 秒,它将超时。 你正在睡觉(10); 它休眠 10 秒,是指定超时时间的两倍。 解决此问题的方法是执行以下操作:

// client 
$.ajax({
    type: "GET",
    url: "some.php",
    data: "name=John&location=Boston",
    timeout: 10000,

    success: function(msg){
        alert( "Data Saved: " + msg );
    },
    error: function(request, errorType, errorThrown){
        alert("opppsssss .... ");
    }
});

服务器代码:

//some.php
< ?
    //simulate long task
    sleep(5); // sleep 5 seconds!
    //send response
    echo "some test data";
? >

希望有帮助。

You're doing it back-to-front by the looks of it. You've set the timeout to 5000 milliseconds (5 seconds) so if the server-side code takes longer to respond than 5 seconds it will timeout. You're doing sleep(10); which sleeps for 10 seconds, which is twice as long as the specified timeout. The way to fix this would be to do the following:

// client 
$.ajax({
    type: "GET",
    url: "some.php",
    data: "name=John&location=Boston",
    timeout: 10000,

    success: function(msg){
        alert( "Data Saved: " + msg );
    },
    error: function(request, errorType, errorThrown){
        alert("opppsssss .... ");
    }
});

And the server code:

//some.php
< ?
    //simulate long task
    sleep(5); // sleep 5 seconds!
    //send response
    echo "some test data";
? >

Hopefully that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文