jQuery AJAX在另一个响应后停止请求

时间:2017-01-27 17:27:56

标签: javascript jquery ajax asynchronous

我有一个问题,我需要一个解决的想法:)

我有2次调用$ .ajax 首先,是asynch,并且在很长一段时间内(例如1分钟) 第二,是同步(在ajax async:false中)并且它快速响应(例如5秒)

第二次呼叫处于循环中(请求 - >响应 - >打印数据,请求 - >响应 - >打印数据)。 我需要在第一次完成(成功或错误)时,停止第二次通话。

我附上了一个示例代码:

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);

我希望它清楚:))

4 个答案:

答案 0 :(得分:0)

我纠正了会导致一些错误的问题,请尝试一下。

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' );
    }
})

答案 1 :(得分:0)

您可以使用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');
});

答案 2 :(得分:-1)

您可以像这样更改成功值

order

或者你可以创建一个自我调用函数(在第二次ajax完成之后,再次调用它)但在调用之前它会像@mplungjan那样检查成功变量。

答案 3 :(得分:-1)

循环Ajax永远不是一个好主意。您需要允许呼叫返回。 这是一个不使用async false的示例

&#13;
&#13;
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();
&#13;
&#13;
&#13;

相关问题