Javascript 在连续的AJAX调用中使用async
这里的问题是关于连续AJAX调用和异步的使用。这里有点混乱,因为数据是如何设置的。我需要返回列表,但是服务器每次查询只返回10个,确定列表总数的唯一方法是使用布尔值returnTotal作为true而不是false的单独查询。这只返回列表的数量,而不返回列表结果本身。但是,如果我同步运行调用,变量startItem(在每个循环中递增以加载从下一个列表块开始的数据)似乎不会在下一个调用完成之前填充,并且结果会重复。有没有办法避免以异步方式运行两者?如果我的代码太荒谬,我会道歉,因为我对jquery比较陌生Javascript 在连续的AJAX调用中使用async,javascript,jquery,Javascript,Jquery,这里的问题是关于连续AJAX调用和异步的使用。这里有点混乱,因为数据是如何设置的。我需要返回列表,但是服务器每次查询只返回10个,确定列表总数的唯一方法是使用布尔值returnTotal作为true而不是false的单独查询。这只返回列表的数量,而不返回列表结果本身。但是,如果我同步运行调用,变量startItem(在每个循环中递增以加载从下一个列表块开始的数据)似乎不会在下一个调用完成之前填充,并且结果会重复。有没有办法避免以异步方式运行两者?如果我的代码太荒谬,我会道歉,因为我对jquery
$.ajax({
type: "POST",
url:server url here,
data:"creativeID=test&CompanyId=BHSR&StartItem=0&streetlocation="+choiceTown+"&Location="+sectCode+"&PriceMin="+choiceMin+"&PriceMax="+choiceMax+"&ListingType="+checkRB+"&OpenHouse=false&NewDev=false&AuthenticationId=id&ReturnTotal=true",
dataType: "html",
async: false,
success: function(data) {
data=convert(data);
$(data).find('Listing').each(function(){
$(this).find('total').each(function(){
totalList = $(this).text();
totalList = parseInt(totalList);
totalPages = totalList/10;
});
});
},
});
for (i = 0; i < totalPages; i++){
$.ajax({
type: "POST",
url:server url here,
data:"creativeID=test&CompanyId=BHSR&StartItem="+startItem+"&streetlocation="+choiceTown+"&Location="+sectCode+"&PriceMin="+choiceMin+"&PriceMax="+choiceMax+"&ListingType="+checkRB+"&OpenHouse=false&NewDev=false&AuthenticationId=id&ReturnTotal=false",
dataType: "html",
success: function(data) {
data=convert(data);
$(data).find('Listing').each(function(){
results_xml.push($(this));
});
result_index=0;
result_image_counter=1;
startItem = startItem + 10;
popResults();
},
});
}
$.ajax({
类型:“POST”,
url:此处的服务器url,
数据:“creativeID=test&CompanyId=BHSR&StartItem=0&streetlocation=“+choiceTown+”&Location=“+sectCode+”&PriceMin=“+choiceMin+”&PriceMax=“+choiceMax+”&ListingType=“+checkRB+”&OpenHouse=false&NewDev=false&AuthenticationId=id&ReturnTotal=true”,
数据类型:“html”,
async:false,
成功:功能(数据){
数据=转换(数据);
$(数据).find('列表').each(函数(){
$(this).find('total').each(function(){
totalList=$(this.text();
totalist=parseInt(totalist);
totalPages=totalList/10;
});
});
},
});
对于(i=0;i
您的意思是没有async:false吗
$.ajax({
type: "POST",
url:server url here,
data:"creativeID=test&CompanyId=BHSR&StartItem=0&streetlocation="+choiceTown+"&Location="+sectCode+"&PriceMin="+choiceMin+"&PriceMax="+choiceMax+"&ListingType="+checkRB+"&OpenHouse=false&NewDev=false&AuthenticationId=id&ReturnTotal=true",
dataType: "html",
success: function(data) {
console.log('test1'); // first response ok
data=convert(data);
$(data).find('Listing').each(function(){
$(this).find('total').each(function(){
totalList = $(this).text();
totalList = parseInt(totalList);
totalPages = totalList/10;
});
});
var startItem=0;
console.log(totalPages); // total page should be equal too "loop" logged
for (i = 0; i < totalPages; i++){
console.log('loop'); // enter the loop
$.ajax({
type: "POST",
url:server url here,
data:"creativeID=test&CompanyId=BHSR&StartItem="+startItem+"&streetlocation="+choiceTown+"&Location="+sectCode+"&PriceMin="+choiceMin+"&PriceMax="+choiceMax+"&ListingType="+checkRB+"&OpenHouse=false&NewDev=false&AuthenticationId=id&ReturnTotal=false",
dataType: "html",
success: function(data) {
console.log('test2'); // number of test 2 = nb of loop = totalPages
data=convert(data);
$(data).find('Listing').each(function(){
results_xml.push($(this));
});
result_index=0;
result_image_counter=1;
startItem = startItem + 10;
popResults();
},
});
}
},
});
$.ajax({
类型:“POST”,
url:此处的服务器url,
数据:“creativeID=test&CompanyId=BHSR&StartItem=0&streetlocation=“+choiceTown+”&Location=“+sectCode+”&PriceMin=“+choiceMin+”&PriceMax=“+choiceMax+”&ListingType=“+checkRB+”&OpenHouse=false&NewDev=false&AuthenticationId=id&ReturnTotal=true”,
数据类型:“html”,
成功:功能(数据){
console.log('test1');//第一次响应正常
数据=转换(数据);
$(数据).find('列表').each(函数(){
$(this).find('total').each(function(){
totalList=$(this.text();
totalist=parseInt(totalist);
totalPages=totalList/10;
});
});
var-startItem=0;
console.log(totalPages);//总页面应该与记录的“循环”相同
对于(i=0;i
这里的问题是,在收到响应之前,您不会增加startItem
。在收到第一个响应之前,您的代码可能会发出多个带有startItem===1
的请求,因此您会得到一些非常奇怪的行为(可能会得到重复的响应,并且您只会得到前几页的数据)
避免使用同步调用,因为它们会占用其他资源(如javascript)
在这种情况下,如果您希望确保按顺序获取数据,可以将其设置为一系列AJAX调用
为了获得串行行为并享受AJAX的好处,不要使用循环,而是让回调函数在递增startItem
后执行下一个AJAX请求
如果将代码组织到函数中,这会更容易。也就是说:
function GetData()
{
$.ajax({
type: "POST",
url:server url here,
data:"creativeID=test&CompanyId=BHSR&StartItem="+startItem+"&streetlocation="+choiceTown+"&Location="+sectCode+"&PriceMin="+choiceMin+"&PriceMax="+choiceMax+"&ListingType="+checkRB+"&OpenHouse=false&NewDev=false&AuthenticationId=id&ReturnTotal=false",
dataType: "html",
success: GetData_Callback
});
}
function GetData_Callback(data)
{
data=convert(data);
$(data).find('Listing').each(function(){
results_xml.push($(this));
});
result_index=0;
result_image_counter=1;
startItem += 10; // increment startItem
popResults();
if (startItem / 10 < totalPages)
{
GetData(); // get next "page" of data
}
}
var startItem = 1; // global variable will be mutated by GetData_Callback
GetData(); // get first "page" of data
函数GetData()
{
$.ajax({
类型:“POST”,
url:此处的服务器url,
数据:“creativeID=test&CompanyId=BHSR&StartItem=“+StartItem+”&streetlocation=“+choiceTown+”&Location=“+sectCode+”&PriceMin=“+choiceMin+”&PriceMax=“+choiceMax+”&ListingType=“+checkRB+”&OpenHouse=false&NewDev=false&AuthenticationId=id&ReturnTotal=false”,
数据类型:“html”,
成功:GetData\u回调
});
}
函数GetData\u回调(数据)
{
数据=转换(数据);
$(数据).find('列表').each(函数(){
结果_xml.push($(this));
});
结果_指数=0;
结果\图像\计数器=1;
startItem+=10;//增加startItem
popResults();
如果(startItem/10<总页数)
{
GetData();//获取下一页数据
}
}
var startItem=1;//全局变量将由GetData\u回调进行变异
GetData();//获取数据的第一个“页面”
在宾夕法尼亚州这样做
var pages = [];
var totalPages = GetTotalPages(); // request via ajax like you mentioned (function not shown)
var pagesLoaded = 0;
for(var i = 0; i < totalPages; i++)
{
GetData(pageIdx);
}
function GetData(pageIdx)
{
$.ajax({ ..., success: function(data){GetData_Callback(pageIdx,data);}});
}
function GetData_Callback(pageIdx, data)
{
pages[pageIdx] = data; // assign this specific page of data
pagesLoaded++;
if (pagesLoaded === totalPages)
{
// fully loaded; trigger event or call function to render, etc.
}
}
$.ajax({
type: "POST",
url:server url here,
data:"creativeID=test&CompanyId=BHSR&StartItem=0&streetlocation="+choiceTown+"&Location="+sectCode+"&PriceMin="+choiceMin+"&PriceMax="+choiceMax+"&ListingType="+checkRB+"&OpenHouse=false&NewDev=false&AuthenticationId=id&ReturnTotal=true",
dataType: "html",
async: false,
success: function(data) {
data=convert(data);
$(data).find('Listing').each(function(){
$(this).find('total').each(function(){
totalList = $(this).text();
totalList = parseInt(totalList);
totalPages = totalList/10;
});
});
},
});
var startItem = 1;
for (i = 0; i < totalPages; i++){
$.ajax({
type: "POST",
url:server url here,
data:"creativeID=test&CompanyId=BHSR&StartItem="+startItem+"&streetlocation="+choiceTown+"&Location="+sectCode+"&PriceMin="+choiceMin+"&PriceMax="+choiceMax+"&ListingType="+checkRB+"&OpenHouse=false&NewDev=false&AuthenticationId=id&ReturnTotal=false",
dataType: "html",
success: function(data) {
data=convert(data);
$(data).find('Listing').each(function(){
results_xml.push($(this));
});
result_index=0;
result_image_counter=1;
popResults();
},
});
// increment start item BEFORE the next request, not in the response
startItem += 10; // now the next request will be for 11, 21, 31, 41, etc...
}