【问题标题】:jquery two ajax call asynchrounsly in asp.net not workingjquery在asp.net中异步调用两个ajax调用不起作用
【发布时间】:2010-05-07 11:16:36
【问题描述】:

我在 asp.net 中开发了一个 Web 应用程序。在这个应用程序中,我在某些页面上使用了 jquery ajax。在这个应用程序中,当我异步进行两个 ajax 调用时,这不会像我预期的那样。发生的事情甚至是第二个 ajax 调用完成我可以在最大超时 ajax 调用完成时看到结果。我的意思是我可以同时看到这两个结果,而不是一个一个。

举个例子。我有 3 页

  1. main.aspx - 用于发出两个 ajax 请求。

  2. totalCount.aspx - 查找总计数。 (最多需要 7 秒才能返回,因为对应的表包含 30 万条记录)

  3. rowCount.aspx - 查找行详细信息。 (返回结果最多需要 5 秒)。

由于这个场景,我计划在 asp.net 中的 jquery ajax 中进行 asyn 调用。 这是我的代码:

function getResult() {
           getTotalCount();
           getRows();
}

// it takes max 7 seconds to complete
// as it take 7 seconds it should display second.( I mean after the rows dispaying)
// but displaying both at the same time after the max time consuming ajax call completed.
function getTotalCount() {
     $.ajax({
               type : "POST",
               async : true,
               url : "totalCount.aspx?data1=" + document.getElementById("data").value,
               success : function(responseText) {                    
                 $("#totalCount").attr("value", responseText);
               }
            })                     
}

// it takes max 5 seconds to complete.
// after finished, this should display first.( i mean before total count displays)
// but displaying both at the same time after the max time consuming ajax call completed.
function getRows() {     
                $.ajax({
                  type : "POST",
                  url : "getrows.aspx?data1=" + document.getElementById("data").value,
                  async : true,
                  success : function(responseText) {                                          
                     $("#getRows").attr("value", responseText);            
                  }
               });           
}

我想知道,如果有可能在 asp.net 中的 jquery ajax 中进行异步调用。 我在网上搜索,我得到了一些点说我们不能在 asp.net 中这样做

参考链接:http://www.mail-archive.com/jquery-en@googlegroups.com/msg55125.html

如果我们可以在asp.net中做到这一点怎么做?

【问题讨论】:

    标签: javascript asp.net jquery ajax asynchronous


    【解决方案1】:

    我不是 asp.net 开发人员,但在一般的 ajax 术语中,我可以解释这个问题,如下所示

    就 ajax 而言,它是异步的,因此您期望在某些特定的方式中的两个请求是不明智的。

    给出的时间将是数据库服务器执行某些查询所花费的时间,但还有更多的请求,例如网络流量等,因此您假设 1 个请求将在其他请求之前返回是不正确的。

    【讨论】:

    • 但是,为什么要同时返回两个结果。如果其中任何一个先完成,则它会显示给它的相应控件。我非常确定时机。所以,我可以直截了当地说,一个人应该先完成....
    【解决方案2】:

    .NET 会话状态将导致对特定会话的所有请求都排队。

    如果不需要会话状态,可以disable it

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 2014-08-13
      相关资源
      最近更新 更多