等待AJAX​​调用(POST)来完成 [英] Wait for AJAX Call (POST) to Finish

查看:160
本文介绍了等待AJAX​​调用(POST)来完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery UI选项卡,每个卡都有不同的形式就可以了。在用户输入各种数据,他们提交的整套标签,以便每个标签的帖子到服务器异步。这种运作良好,我这里没有问题。

I am using the jQuery UI tabs where each tab has a different form on it. After the user enters various pieces of data, they submit the entire set of tabs so that each tab posts to the server asynchronously. This works well, and I have no problems here.

然而,当我遇到一个问题是,我张贴有过去的形式发生的所有其他职位完成后。总的思路是这样的:

However, where I run into a problem is that, the last form that I post has to happen AFTER all the other posts complete. General idea is like this:

postForm(0, "#Form1");
postForm(1, "#Form2");
postForm(2, "#Form3");
postForm(3, "#Form4");
$.post('Project/SaveProject', function (data) {
    $('<div class="save-alert">The current project has been saved.</div>')
    .insertAfter($('#tabs'))
    .fadeIn('slow')
    .animate({ opacity: 1.0 }, 3000)
    .fadeOut('slow', function () {
        $(this).remove();
    });
});

在postForm函数做处理一点点,然后做一个AJAX $ .post的电话。这里执行(以项目/ SaveProject)最后$。员额必须等待,直到这些其他职位已经完成。什么是去这样做,最好的方法是什么?

The postForm function does a little bit of processing and then makes an AJAX $.post call. The last $.post performed here (to 'Project/SaveProject') must wait until those other posts are completed. What is the best way to go about doing that?

推荐答案

您可以只使用 .ajaxStop( ) 事件,当所有其他的AJAX的POST完成,假设你正在做的没有其它AJAX工作,那么,这样的火灾:

You can just use the .ajaxStop() event that fires when all the other AJAX POSTs finish, assuming you're doing no other AJAX work then, like this:

postForm(0, "#Form1");
postForm(1, "#Form2");
postForm(2, "#Form3");
postForm(3, "#Form4");
$(document).ajaxStop(function() {
  $.post('Project/SaveProject', function (data) {
    $('<div class="save-alert">The current project has been saved.</div>')
    .insertAfter($('#tabs'))
    .fadeIn('slow')
    .animate({ opacity: 1.0 }, 3000)
    .fadeOut('slow', function () {
      $(this).remove();
    });
  });
  $(this).unbind('ajaxStop');
});

.ajaxStop() 火灾时的所有的运行完成后的所有的这些职位已经返回,jQuery的内部保持一个计数AJAX请求,因此它只会大火( $主动 $。ajax.active 中的jQuery 1.5),以determind同时AJAX请求有多少优秀的...当它得到回到0时触发这个事件。

.ajaxStop() fires when all AJAX requests that are running complete, so it'll only fires when all of those POSTs have returned, jQuery keeps a count internally ($.active, $.ajax.active in jQuery 1.5) to determind how many simultaneous AJAX requests are outstanding...when it gets back to 0 this event fires.

此方法允许后尽快你同时做其他形式提交,并做最后的形式。

This approach lets you do the other form submissions simultaneously, and do the final form ASAP after.

这篇关于等待AJAX​​调用(POST)来完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆