ajax同步调用带超时

发布于 2024-08-20 07:49:06 字数 361 浏览 2 评论 0原文

刚接触ajax,所以问一个非常基本的问题。

-- 有没有办法进行同步 ajax 调用 (async:false) 并设置超时。?

http://www.ajaxtoolbox.com/request/

超时在我的应用程序中与异步调用完美配合, 但对于一个特定的场景,我需要一个同步调用(javascript实际上应该等待,直到它从服务器返回),这也可以正常工作。但我需要处理服务器可能需要很长时间并且可能会调用 ajax 超时的情况。

还有其他关于 ajax 的标准文档我可以参考吗?

谢谢

New to ajax, so asking a very basic question.

-- Is there no way to make a Synchronous ajax call (async:false) with timeout set on it.?

http://www.ajaxtoolbox.com/request/

Timeout works perfect with Asynchronous call though in my application,
but for one particular scenario, I need a Synchronous call (the javascript should actually wait untill it hears back from the server), and this also works fine. But I need to handle a scenario where the sever could take long and a ajax timeout may be called.

Is there any other piece of standard documentation for ajax I could refer to?

Thanks

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

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

发布评论

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

评论(2

美煞众生 2024-08-27 07:49:06

基本上,在同步ajax请求期间,浏览器被阻止,并且在浏览器被阻止时不能执行任何javascript。因此,jQuery 无法在设置的超时后中止 ajax 请求,因为 jQuery 是 javascript,而当浏览器被阻止时,javascript 无法执行。这是同步ajax 的主要缺陷。

任何时候您可能需要同步请求,都应该使用异步请求以及回调中随后发生的事情,如下所示;

$.ajax({
    url : 'webservices.php',
    timeout: 200,
    dataType : 'json',
    data : {
        'cmd' : 'ping',
    },
    success : function(data, textStatus) {
        $.ajax({
            url : 'webservices.php',
            async: false,
            dataType : 'json',
            data : {
                'cmd' : 'feedback',
                'data' : data,
                'userinfo' : window.dsuser
            },
            success : function(data, textStatus) {
                // success!
                Status("Thanks for the feedback, "
                    + window.dsuser.user + "!");
            }
        });
    },
    error : function(jqhdr, textStatus,
                     errorThrown) {
        Status("There was trouble sending your feedback. Please try again later");
    }
});

Basically, during a synchronous ajax request, the browser is blocked and no javascript can be executed while the browser is blocked. Because of this, jQuery can't abort the ajax request after a set timeout because jQuery is javascript and javascript can't be executed while the browser is blocked. This is the primary flaw in synchronous ajax.

Anytime you might want a synchronous request, you should instead use an asynchronous one with what should happen afterwards in the callback, as shown below;

$.ajax({
    url : 'webservices.php',
    timeout: 200,
    dataType : 'json',
    data : {
        'cmd' : 'ping',
    },
    success : function(data, textStatus) {
        $.ajax({
            url : 'webservices.php',
            async: false,
            dataType : 'json',
            data : {
                'cmd' : 'feedback',
                'data' : data,
                'userinfo' : window.dsuser
            },
            success : function(data, textStatus) {
                // success!
                Status("Thanks for the feedback, "
                    + window.dsuser.user + "!");
            }
        });
    },
    error : function(jqhdr, textStatus,
                     errorThrown) {
        Status("There was trouble sending your feedback. Please try again later");
    }
});
楠木可依 2024-08-27 07:49:06

我不认为可以在同步调用上设置超时。当您设置“async:false”时,我相信浏览器在等待响应时实际上会锁定。仅当绝对需要时才应使用同步请求(因为浏览器锁定)。

I don't believe it's possible to set a timeout on a synchronous call. When you set "async:false", I believe the browser actually locks up while waiting for the response. You should only use a synchronous request if you absolutely need to (because of the browser locking up).

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