【问题标题】:Ajax call should not wait for responseAjax 调用不应等待响应
【发布时间】:2012-05-07 23:30:27
【问题描述】:

我在我的经典 asp 应用程序中使用 ajax 调用来执行存储过程。
但我不想等到存储过程运行(存储过程大约需要 5-10 分钟才能完成)。
Ajax 必须调用存储过程并且需要立即返回。
我希望 Ajax 调用不应该等待响应。

这是我的代码 sn-p:

1)  $.ajax({  
  type: "POST",  
  url: "runstoredprocedure.asp",  
});    
2)  setInterval(function(){  jQuery("#list").trigger("reloadGrid"); },10000);  

这是我正在使用的两个 ajax 调用。第一个运行大约 5-7 分钟。第二个在第一个完成之前不会触发。但我需要立即调用第二个 ajax 调用。

谁能帮我解决这个问题。

【问题讨论】:

    标签: ajax


    【解决方案1】:

    javascript 将请求作为不同线程的一部分触发,并且您的 ajax 调用之后的任何代码都将立即执行。说了一个关于 JS async 的误解:

    People take for granted that because it’s asynchronous, it’s a thread. They are partially right. There must be a thread created by the browser to keep the javascript running while it makes a request to the server. It’s internal and you don’t have access to that thread. But, the callback function called when the server responds to the ajax request is not in a thread.
    
    I’ll explain clearer. If javascript runs some code that takes 5 seconds to execute and an ajax response arrives at 2 seconds, it will take 3 seconds before it will be executed (before the callback function is called). That’s because javascript itself doesn’t create a thread to execute the ajax response from the server and simply waits that all executions are terminated before starting a new one.
    
    So if you’re running a lot of ajax requests simultaneously, you might get some weird behavior because they will all wait one on another before executing themselves.
    

    最后一个陈述与你的事业有关。

    摘自博客:http://www.javascriptkata.com/2007/06/04/ajax-and-javascript-dont-use-threads/

    有趣的阅读:http://www.javascriptkata.com/2007/06/12/ajax-javascript-and-threads-the-final-truth/

    【讨论】:

      【解决方案2】:

      AJAX 默认是异步的(它是所有 javascript 库中的默认选项)。例如,在 jQuery 中:

      $.ajax({
        url: url,
        data: data,
        success: success,
        dataType: dataType
      });
      

      你有一个成功,这需要一个回调。当您的操作完成时,将调用回调。 jQuery 将立即返回。

      【讨论】:

      • 我正在使用 falowing $.ajax({ type: "POST", url: "p...te.asp", });但我发现使用 fire bug 这个 ajax 调用运行了很长时间。
      • @vissu pepala,编辑您的问题以添加您正在使用的代码 sn-p。从评论中看不到。
      • 我已编辑。 @Senthess,你现在能看到吗?
      • @vissu pepala,为什么没有成功回调?即使是空的。
      • 有没有办法使用jQuery的ajax而不用等待成功或错误回调?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多