Do while javascript issue

后端 未结 3 1431
没有蜡笔的小新
没有蜡笔的小新 2021-01-16 06:46

I\'m trying to send multiple post within a do while loop but the result is not added



        
3条回答
  •  萌比男神i
    2021-01-16 07:31

    This is because of the async behavior of ajax: Here is a modified version:

    var initval = 1;
    var endval = 5;
    function action(){
        var action_string = 'txtuser=someone';
        $.ajax({
            type: "POST",
            url: "http://localhost/js.php",
            data: action_string,
            success: function(result){
               $('div#append_result').append(initval + ',
    '); initval++; if(initval<=endval) action(); } }); }

    This is now somewhat a sequential approach. Note: I assumed every ajax request returns success, if there is an error, you should handle them on the error callback.

提交回复
热议问题