Jquery 并发ajax调用
我试图在jquery中进行并发ajax调用,但没有找到任何与此相关的具体内容。当我进行两次ajax调用时,只有在第一次调用完成后才启动另一个ajax调用。我尝试了Jquery 并发ajax调用,jquery,ajax,concurrency,Jquery,Ajax,Concurrency,我试图在jquery中进行并发ajax调用,但没有找到任何与此相关的具体内容。当我进行两次ajax调用时,只有在第一次调用完成后才启动另一个ajax调用。我尝试了$.ajaxSetUp{(async:true)},但它没有按照我想要的方式工作。有人能帮我吗 [编辑] 我有一个表单,在表单提交时,我使用的AjaxSubmit,因为我的表单也有文件输入 $("form").submit(function() { $(this).ajaxSubmit(); $.getScript("url")
$.ajaxSetUp{(async:true)}
,但它没有按照我想要的方式工作。有人能帮我吗
[编辑]
我有一个表单,在表单提交时,我使用的AjaxSubmit,因为我的表单也有文件输入
$("form").submit(function() {
$(this).ajaxSubmit();
$.getScript("url");
return false; // to stop the normal working of form submit and submit form by ajax
})
这里,第二个ajax只有在第一个ajax完成后才被调用。因此,您所说的是“竞争条件”。
为了避免这种情况,您有两种选择,要么创建一个数组来存储每个请求,要么创建一个局部变量来存储每个请求。
下面是一个避免竞争条件的简单示例:
function customCallBack()
{// Ajax Status finished http status OK
if( (this.readyState == 4) && (this.status == 200) )
{
alert(this.responseText);
}
}
function anotherCallBack()
{// Ajax Status finished http status OK
if( (this.readyState == 4) && (this.status == 200) )
{
console.log(this.responseText);
}
}
function asyncPost(url, postArray, functionCallBack)
{
var request, query; //request must be a local var to avoid race condition
query = '';
for (i in postArray)//format post params
{
query += i + '=' + postArray[i] + '&';
}
try
{//for modern browsers
request = new XMLHttpRequest;
}
catch (err)
{// legacy IE
request = new ActiveXObject("Microsoft.XMLHTTP");
}
// your custom call back function
request.onreadystatechange = functionCallBack;
// type url true for async call, false for sync call
request.open("POST", url, true);
//Header sent to indicate a post form
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//making a post call
request.send(query);
};
//calling
asyncPost('yourUrl', {'field1' : 'value1', 'field2': 10}, customCallBack);
var post = new Array();
post['field1'] = 'value1';
post['field2'] = 10
//calling againg
asyncPost('anotherUrl', post, anotherCallBack);
// In jquery is like above, because you need the ajax call in a custom function
// and create a local var to hold your call:
function jqAjax(url)
{
var mycall = $.get(url);
}
两个Ajax调用都是独立的,如果第一个调用延迟完成,那么第二个调用将独立继续,您可以通过Ajax进行任意多的调用:10、20、30
要更改方法,请使用下面的函数
function asyncCall(url, method, varArray, functionCallBack)
{
var request, query;
query = '';
for (i in postArray)//format params
{
query += i + '=' + postArray[i] + '&';
}
try
{//for modern browsers
request = new XMLHttpRequest;
}
catch (err)
{// legacy IE
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.onreadystatechange = functionCallBack;
if(method == 'GET')
{
request.open("GET", url + '?' + query, true);
request.send();
}
else
{
request.open("POST", url, true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(query);
}
};
console.log('start 1');
asyncCall('url1', 'GET', {foo : 'bar'}, function(){console.log('Call 1');});
console.log('start 2');
asyncCall('url2', 'POST', {foo : 'bar'}, function(){console.log('Call 2');});
console.log('start 3');
asyncCall('url3', 'POST', {foo : 'bar'}, function(){console.log('Call 3');});
发布你的javascript。对.ajax
的调用不会像您描述的那样计时,除非您明确设置了async:false
。我们需要查看代码-ajax调用不会阻止其他ajax调用,因此会发生其他事情。顺便说一下。。。您的代码实际上并不发送任何ajax请求,而是将一个隐藏表单提交到iframe,并在头部附加一个脚本标记。我不确定这对您的问题到底意味着什么,但它确实增加了可能性。我不理解jqAjax函数的用途。它不在任何地方使用。你能再解释一下吗!您的解决方案适用于异步调用。但它们都同时停止。但是,我希望在另一个ajax请求运行时完成一个ajax请求。此外,我希望第二个ajax调用在第一个运行时是周期性的。有没有办法做到这一点?此外,我的第二个ajax调用是“GET”,因此仅在请求时将“POST”更改为“GET”就足够了