同时发送多个AJAX请求 [英] Send multiple AJAX request concurrently

查看:798
本文介绍了同时发送多个AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个JSP应用程序,我需要使用ajax动态更新另外两个下拉列表。为此我写了两个不同的JS函数,它们为两个不同的URL创建了AJAX请求。当我使用onChange选择控件调用这两个函数中的任何一个时,它们工作得很好。但是当我一次放置它们时失败了。请帮助我如何调用两个JS函数,它们同时向两个不同的URL发送请求。

提前谢谢。

解决方案

我只是猜测,因为你的帖子中没有太多细节。听起来其中一个ajax调用(或两者)依赖于另一个调用的值。如果是这种情况,那么您所期望的是它们按您调用它们的顺序运行。



例如你期待



  function (){
a();
b();
}





a(被调用)会发生什么,然后无需等待它完成b()被调用。如果a()正在设置您需要的值,则b()将失败。您可以关闭异步功能。



对于jquery你可以这样做:



.ajax({

......

url:'...',

data:dataStuff,

async:false

........

});





然后b()在a()完成之前不会运行。


其实我没用我的应用程序中的jQuery。另一件事是我使用相同的XMLHttpObject引用变量,通过思考JS函数完成后不久,局部变量将过期,但它没有发生。我尝试改变参考变量,它运作良好。

谢谢你们回复我的问题。

Hi, I am developing a JSP application, in which I need to update two more drop down lists dynamically using ajax. For this I wrote two different JS Functions which creates AJAX request to two different URLs. They are working good when I called any one of those two functions with onChange of select control. But failed when I placed both of them at a time. Please help me how I can call two JS functions, which sends request to two different URLs concurrently.
Thank you in advance.

解决方案

I'll just take a guess since there wasn't much detail in your post. It sounds like one of the ajax calls (or both) is relying on values set by the other. If this is the case then what you are expecting is them to run in the order you called them.

For instance you are expecting

function(){
    a();
    b();
}



What happens is a() gets called, and then without waiting for it to finish b() gets called. If a() is setting a value that you need, then b() will fail. You can turn the async functionality off.

For jquery you could do:


.ajax({
......
url: '...',
data: dataStuff,
async:false,
........
});


and then b() would not run until a() is finished.


Actually I am not using jQuery in my application. And another thing is I am using the same reference variable for XMLHttpObject by thinking as soon after completion of of JS function, the local variables will expire, but it didn't happened. I experimented with changing the reference variable, it worked good.
Thankyou all for responding my problem.


这篇关于同时发送多个AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆