[英]jQuery AJAX stop request after another response
我有一个问题,我需要一个解决的主意:)
我有2个调用$ .ajax的电话,首先是异步的,并且在很多时间(例如1分钟)内,其次是sync(在ajax async:false)中,并且响应速度很快(例如5 sec)
第二次调用处于循环中(请求->响应->打印数据,请求->响应->打印数据)。 我需要第一次完成(成功或错误)时,停止第二次通话。
我附上示例代码:
var success = false;
$.ajax({
type: "POST",
url: urlRest,
data: {
data: dataSend
},
success: processOK,
error: processError
});
do {
$.ajax({
type: "POST",
url: urlData,
data: {
data: dataSend
},
async: false,
success: function(data, textStatus, jqXHR){
console.log(data);
},
error: function(data, textStatus, jqXHR){
console.log("Error");
}
});
} while (!success);
我希望很清楚:)
我更正了会导致一些错误的问题,请尝试一下。
let printData = function( input ){
let config = {
urlRest: '',
data: { data: {} },
loop: false,
callback: false
}
$.each(config,function(k,v){ config[k] = input[k] });
config.loop = false;
$.ajax({
type: 'POST',
url: config.urlRest,
data: config.data,
success: function( data ){
// Based on the response if you need to run again change config.loop to true and it will run again
// you can also alter anything your sending through
if( config.loop ) printData( config );
else if( typeof config.callback === 'function' ) callback();
},
error: function(){
// Based on the response if you need to run again change config.loop to true and it will run again
// you can also alter anything your sending through
if( config.loop ) printData( config );
else if( typeof config.callback === 'function' ) callback();
}
});
}
printData({
urlRest: '', // URL Here
data: data, // Data Object
loop: true, // Set this to true if you want it to loop
callback: function(){
console.log( 'Job Complete' );
}
})
您可以使用SynJS以同步方式运行异步调用:
function ajaxWrapper(ctx, url, data){
var res={done:false};
$.ajax({
type: "POST",
url: url,
data: data,
success: function(result){
res.data=result;
},
error: function(){
res.error=true;
},
}).always(function(){
res.done = true;
SynJS.resume(ctx); // <-- tell caller that callback is finished
});
return res; // <-- return object that will hold the results
}
// function that is executed in synchronous manner
function myFunc(modules, urlRest, urlData) {
var success = false;
var res1 = modules.ajaxWrapper(_synjsContext, urlRest, urlData);
SynJS.wait(res1.done); // <-- wait for result from callback
do {
var res2 = modules.ajaxWrapper(_synjsContext, urlRest, urlData);
SynJS.wait(res2.done); // <-- wait for result from 2nd callback
} while (!success);
}
var modules = {ajaxWrapper: ajaxWrapper};
SynJS.run(myFunc,null, modules, "/", {}, function () {
console.log('done');
});
您可以像这样更改成功值
$.ajax({
type: "POST",
url: urlRest,
data: {
data: dataSend
}
}).always(function() {success=true;});
或者,您可以创建一个自调用函数(在第二个ajax完成之后,再次调用它),但是在调用之前,它会像@mplungjan一样检查成功变量。
循环使用Ajax绝不是一个好主意。 您需要允许呼叫返回。 这是一个不使用异步false的示例
var firstDone = false,tId; // call long ajax $.ajax({ type: "POST", url: urlRest, data: { data: dataSend } }).done(processOK); }).fail(processError) }).always(function() {firstDone=true; clearTimeout(tId);}); // stops the other loop // setup function that can be looped function callAjax() { if (firstDone) return; $.ajax({ type: "POST", url: urlData, data: { data: dataSend } }).done(function(data, textStatus, jqXHR) { console.log(data); }).fail(function(data, textStatus, jqXHR) { console.log("Error"); }).always(function() { tId=setTimeout(callAjax,1000); // give the server time to recover }); } callAjax();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.