Javascript 管道化ajax http请求并仅对最新请求执行成功函数
我正在使用jQueryAJAXAPI调用一个web方法,在Javascript 管道化ajax http请求并仅对最新请求执行成功函数,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在使用jQueryAJAXAPI调用一个web方法,在success上,我正在实现一个类似showHideDomElement的功能 function showHideDomElement(data) { if(data == 1 ) $('#selector').show(); else $('#selector').hide(); } 这就是我所说的 function Validatebatch() { $.ajax
success
上,我正在实现一个类似showHideDomElement
的功能
function showHideDomElement(data)
{
if(data == 1 )
$('#selector').show();
else
$('#selector').hide();
}
这就是我所说的
function Validatebatch() {
$.ajax({
type: "POST",
url: getControllerURL("/Invoice") + "/ValidateBatch",
data: {
"Id": $('#someselector').val()
},
async: true, // i tried with 'false' but it affect performance
dataType: "json",
success: function(data) {
showHideDomElement(data);
}
});
}
对Validatebatch
函数的ajax请求多次引发,因此有多个活动http请求指向url
在不同时间完成
由于异步行为,成功回调
可以按不同的顺序执行
完成的顺序造成了问题。请建议一种可以将ajax请求绑定为按执行顺序执行/完成的方式(请建议jqueryasync
property以外的方式)
感谢首先,您必须重写Validatebatch函数,以便它返回由$.ajax调用生成的承诺,例如:
function Validatebatch() {
return $.ajax({
然后,您必须存储每个Validatebatch调用的承诺,并仅在前一个Validatebatch调用完成后才使用它触发下一个Validatebatch,您应该具有以下功能:
promise = Validatebatch(); // call Validatebatch the first time and store the returned Promise object
promise = promise.then(Validatebatch); // another call to Validatebatch, only triggered after the previous has ended
promise = promise.then(Validatebatch); // again, this third Validatebatch call will only happen after the second one has ended.
如何以及在何处启动
Validatebatch()
?