同步Ajax调用——或者:如何等到一切都加载之后才继续?

时间:2022-08-23 18:18:08

I have a single page application with ASP.Net and JavaScipt / jQuery.

我有一个使用ASP的单页面应用程序。Net和JavaScipt / jQuery。

In my Page initialisation there is much to load, e.g. 6 dropdowns, a bunch of textboxfilles, configuration files etc.

在我的页面初始化中有很多要加载的内容,例如6个下拉列表、一堆文本框文件、配置文件等等。

Now I have a gridview to load. The problem ist, that the gridview needs data from the textboxes and dropdowns.

现在我有一个gridview要加载。问题是,gridview需要来自文本框和下拉框的数据。

So how to know, when the asynchron ajax calls of everything else is finished?

那么,如何知道其他所有调用的异步ajax调用何时完成呢?

Simply put a settimeout() for the loading grid and wait a bunch of time, e.g. 300ms?

只需为加载网格输入settimeout()并等待一段时间,例如300ms?

2 个解决方案

#1


3  

You can use callback function which will get called when response of ajax request arrives.

您可以使用回调函数,当ajax请求的响应到达时,回调函数将被调用。

$.ajax({
    type:"method_type",
    url: "your_url",
    dataType: "data_type",
    async: true,
    success: function(data){
        callback(data);
    }
});

function callback(d){
  // grid functionality
}

Update:

更新:

$.when($.ajax("req1"), $.ajax("req2"))
  .then( callback, errfun);

This will execute the function callback when both ajax requests are successful, or errfun if either one has an error.

这将在两个ajax请求成功时执行回调函数,或者如果其中一个有错误,就会出错。

#2


0  

There should be a success callback. In asynchronous programming, you stick event-driven code inside callback functions.

应该会有一个成功的回调。在异步编程中,在回调函数中插入事件驱动代码。

#1


3  

You can use callback function which will get called when response of ajax request arrives.

您可以使用回调函数,当ajax请求的响应到达时,回调函数将被调用。

$.ajax({
    type:"method_type",
    url: "your_url",
    dataType: "data_type",
    async: true,
    success: function(data){
        callback(data);
    }
});

function callback(d){
  // grid functionality
}

Update:

更新:

$.when($.ajax("req1"), $.ajax("req2"))
  .then( callback, errfun);

This will execute the function callback when both ajax requests are successful, or errfun if either one has an error.

这将在两个ajax请求成功时执行回调函数,或者如果其中一个有错误,就会出错。

#2


0  

There should be a success callback. In asynchronous programming, you stick event-driven code inside callback functions.

应该会有一个成功的回调。在异步编程中,在回调函数中插入事件驱动代码。