我使用jQuery AJAX请求动态获取数据。 Ajax调用是嵌套的,它们在每个先前请求的success
函数中被调用。我这样做是为了不把太多的负载放在服务器上。下一个请求只应发送,如果前一个是已完成或成功。根据条件线性执行请求
这里是Ajax代码
function showforLanguagetra(option, catid, meta)
{
$.ajax({ ///execute only if catid = 1,3,6,8
type:"GET",
url: "/Ajax1111",
data: {
opt: option,
cid: catid,
mta: meta
},
success: function(data){
$("#1111").html(data);
$.ajax({ ///execute only if catid = 5,7,9
type:"GET",
url: "/Ajax2222",
data: {
opt: option,
cid: catid,
mta: meta
},
success: function(data){
$("#2222").html(data);
$.ajax({ ///execute only if catid = 2,5,4,8
type:"GET",
url: "/Ajax3333",
data: {
opt: option,
cid: catid,
mta: meta
},
success: function(data){
$("#3333").html(data);
$.ajax({ ///execute only if catid = 6,4,8,9,0
type:"GET",
url: "/Ajax4444",
data: {
opt: option,
cid: catid,
mta: meta
},
success: function(data){
$("#4444").html(data);
$.ajax({ ///execute only if catid = 2,3,5,7,4
type:"GET",
url: "/Ajax5555",
data: {
opt: option,
cid: catid,
mta: meta
},
success: function(data){
$("#5555").html(data);
}
});
}
});
}
});
}
});
}
});
}
此代码工作正常!
但是这里需要的是,我想要执行一个基于在catid中的值的ajax请求,如ajax代码中的注释所示。 我知道,如果条件是滞后于但是是新来的jQuery AJAX所以不知道在哪里以及如何使用它
对我来说这个问题跟jQuery或AJAX无关。你不能只在你的$ .ajax调用周围使用if-blocks吗? 'if([1,3,6,8] .indexOf(catid)> -1){$ .ajax(...'等等? – johusman 2012-07-23 08:54:46
如果条件为false,它会停止所有嵌套的请求 – 2012-07-23 09:10:31
啊,你想要考虑后续的请求,即使不应该做一些? – johusman 2012-07-23 09:13:38