【问题标题】:Make multiple ajax using Jquery in a sequence按顺序使用Jquery制作多个ajax
【发布时间】:2013-08-27 03:14:18
【问题描述】:

我正在使用一个非常糟糕的端点...所以请理解我无法更改此端点的行为方式。

这个 ajax 调用是:POST /doStupidThing/。

给定一个 id 列表,我需要为每个 id 调用端点。 如何确保在上一个呼叫完成之前不会触发呼叫?我应该将 async 设置为 false 吗?

【问题讨论】:

  • 为什么要在这种情况下禁用异步处理,有更多的调用正是您想要利用异步处理的地方。如果数字太大,我可以理解您可能需要限制并发连接数,但对我来说似乎倒退了。
  • 后端代码不是线程安全的。

标签: jquery ajax


【解决方案1】:

是的,您可以尝试将 async 设置为 false

【讨论】:

    【解决方案2】:

    Just for the record, the better way to do things would be to set yourself up to use the Jquery promise interface

    This SO answer covers this in more detail and talks about deferred objects.

    您可以做的一件事是在 .done 函数中进行下一次 ajax 调用。

    function SecondAjax() { 
    $.ajax({
          type: "POST",
          url: "example.php",
          data: data_obj,
          dataType: "html"
       }).done(function(msg) {
             #You can put a third one in here
             #This should also give you an idea of how to loop.
          }
    }
    
    function firstAjax() { 
    $.ajax({
          type: "POST",
          url: "example.php",
          data: data_obj,
          dataType: "html"
       }).done(function(msg) {
             SecondAjax()
          }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多