2011-07-05 153 views
2

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

这里是我的代码片段:

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

这些都是我使用两个Ajax调用。第一个约5-7分钟。第二个在第一个完成之前不会开火。但是立刻我需要调用第二个Ajax调用。

任何人都可以帮助我解决这个问题。

回答

2

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

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

您已成功,需要回调。当你的动作完成时,回调将被调用。 jQuery将立即返回。

+0

我usingthe falowing $阿贾克斯({ 类型: “POST”, 网址: “P ... te.asp” });但我发现使用fire bug这个ajax调用运行很长时间。 – vissu

+0

@vissu pepala,编辑您的问题以添加您正在使用的代码段。这是从评论中看不到的。 – Senthess

+0

我已编辑。 @Senthess,你现在能看到吗? – vissu

3

javascript会将请求作为不同线程的一部分触发,并且您的ajax调用之后的任何代码将立即执行。话虽如此,有关JS异步的一种误解:

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/