如何逐个执行ajax请求

Nar*_*esh 2 ajax jquery

我遇到了这个我无法解决的问题.部分是因为我无法用正确的术语来解释它.我是新手,对这个笨拙的问题感到抱歉.

您可以在下面看到我的目标概述.

这是我的脚本结构..&它的工作正常

<script>
    $(document).ready(function(){
        applyScript();
        var jqueryarray = <?php echo json_encode($array); ?>;
        for (i = 0; i < jqueryarray.length; i++) {
             // my logic is here
             try {
                $.ajax({
                   url: url1,
                   cache:  false ,
                   type : 'POST',
                   // dataType: "json",
                   data: data1,
                   success: function(response){
                      if (response) {
                         // some logic with response
                      }
                   }
                });
             }catch (e) { }
        };
    });
</script>
Run Code Online (Sandbox Code Playgroud)

这里基于for循环其工作ajax发送请求但我的目标是我要一个接一个地发送ajax请求.

你可以在这看到我的概率

在此输入图像描述

有任何想法吗 ?

hi0*_*34d 6

创建一个类似递归的函数,在完成一个ajax请求后调用,如下所示:

   $(document).ready(function(){
    applyScript();
    var jqueryarray = <?php echo json_encode($array); ?>;
    var $i = 0; 
    ajax_forloop( $i );
   });
    function ajax_forloop( $i )
    {
         // my logic is here
         try {
            $.ajax({
               url: url1,
               cache:  false ,
               type : 'POST',
               // dataType: "json",
               data: data1,
               success: function(response){
                  if (response) {
                     // some logic with response


                     //call another ajax request
                     $i++;
                     if( $i < jqueryarray.length ) 
                     {
                         ajax_forloop( $i );  
                     }

                  }
               }
            });
         }catch (e) { }

    }
Run Code Online (Sandbox Code Playgroud)