使jQuery等待$.post完成更新页面

使jQuery等待$.post完成更新页面,jquery,function,.post,Jquery,Function,.post,我正在调用一个函数,用jQuery.post刷新当前页面的一部分,然后在该函数完成后,我需要执行另一个函数,使用从$.post写入的更新HTML更新该页面上的Google地图 我无法嵌套这些函数,因为DoGoogleMap()不在RefreshSubscriptionList()函数的作用域内工作 如何使它在调用DoGoogleMap()函数之前等待$.post将更新的HTML写入页面 function RefreshSubdivisionList() { var formAction

我正在调用一个函数,用jQuery.post刷新当前页面的一部分,然后在该函数完成后,我需要执行另一个函数,使用从$.post写入的更新HTML更新该页面上的Google地图

我无法嵌套这些函数,因为DoGoogleMap()不在RefreshSubscriptionList()函数的作用域内工作

如何使它在调用DoGoogleMap()函数之前等待$.post将更新的HTML写入页面

function RefreshSubdivisionList() {

    var formActionScript = jQuery("#RefreshSubdivisionForm").attr('action');

    jQuery.post(formActionScript, jQuery("#RefreshSubdivisionForm").serialize()).done(function(data) {

        jQuery(".subdivision-list").html(data).show();

    }, 'text');

    return true;

}


jQuery("#RefreshSubdivisionForm").submit(function(event) {

    event.preventDefault();
    jQuery.when( RefreshSubdivisionList() ).done(function(){
        jQuery('#map_canvas').gmap('destroy');
        DoGoogleMap();
    });

    return false;

});

您可以放置
DoGoogleMap()post
done
回调中调用code>,可以吗

然后在post完成后加载地图。

提供了处理同步多个异步调用的机制。看一看:

function ajax1 {
    return $.ajax("/page1/action", {});
}

function ajax2 {
    return $.ajax("/page2/action", {});
}

var promise = $.when(ajax1, ajax2);

promise.done(function (resp1, resp2) {
  // The parameters resp1 and resp2 are the responses 
  // of their respective ajax1 and ajax2 calls.
  // Each parameter is an array that contains the ajax response
  // with the following signature:
  // [data, statusText, jqXHR]
  // See: http://api.jquery.com/jquery.ajax/#jqXHR

  // suppose the data response for ajax1 is: "Hello"
  // suppose the data response for ajax2 is: "world"
  var data = resp1[0] + " " + resp2[0];

  if ((/hello\sworld/i).test(data)) {
    console.info("Promises rules!");
  }
});
在上一个示例中,我们处理成功响应,但我们可以以同样的方式处理失败响应

实现承诺接口返回的对象,为它们提供的所有属性、方法和行为(有关更多信息,请参阅)

另一种方法是创建对象,并使用预期结果解析每个延迟对象,最后统一解析的响应:

var d1 = $.Deferred();
var d2 = $.Deferred();

$.when(d1, d2).done(function (resp1, resp2) {
    console.log(resp1); // "Fish"
    console.log(resp2); // "Pizza"
});

d1.resolve("Fish");
d2.resolve("Pizza");

要实现这一点,您所需要做的似乎就是从
refreshSubscriptionList
方法返回jqXHR对象

jQuery提供了类似于
post
的XHR请求
defered
接口,这是
方法在确定请求状态时使用的接口。如果它完成了或失败了,等等

function RefreshSubdivisionList() {
    var formActionScript = jQuery("#RefreshSubdivisionForm").attr('action');

    var postObj = jQuery.post(formActionScript, jQuery("#RefreshSubdivisionForm").serialize());

    postObj.done(function(data) {
        jQuery(".subdivision-list").html(data).show();
    }, 'text');

    return postObj;
}


jQuery("#RefreshSubdivisionForm").submit(function(event) {
    event.preventDefault();
    jQuery.when( RefreshSubdivisionList() ).done(function(){
        jQuery('#map_canvas').gmap('destroy');
        DoGoogleMap();
    });

    return false;

});

起初我尝试它时它不起作用,但现在它似乎起作用了。我认为在另一个函数中运行该函数会出现范围问题,但现在它工作正常。古怪的谢谢