如何使用javascript从嵌套的ajax调用中将值推送到循环外的数组中

时间:2017-02-27 04:36:32

标签: javascript jquery arrays ajax

我有一个循环,它循环大约16次,每次循环我想要一个值被推送到我在循环外声明的数组,并且数组是从循环中的嵌套ajax调用推送的。我写了一个if条件,如果循环达到16次,它应该立即警告数组的所有内容,但我得到一个空值

var comment = [];
for (var i = 0; i < jsons.length; i++){
   var obj = jsons[i];
   if( i == jsons.length - 1 )
   {
       alert(comment);
       break;                       
   }
   var idno = '';
   idno = obj.IDNO;
   var amount = obj.AMOUNT;
   (function(i)
   {
        $.ajax({
            type: "POST",
            url: "../include/salprefix.php",
            data: {idnoauth: obj.IDNO, amount: amount, },
            dataType: 'json',
            cache: false,
            success: function(result){
                if(result[0].idno == '')
                {
                    alert(result[0].idno + " " + result[0].amount);

                }
                else if(result[0].idno != ''){
                    var idnoresult = result[0].idno;
                    var amountresult = result[0].amount;
                    $.ajax({
                        type: "POST",
                        url: "../include/salprefix.php",
                        data: {idnoinsert: idnoresult, amountinsert: amountresult},
                        cache: false,
                        success: function(gg){
                            if(gg != '')
                            {
                                comment[i] = gg;
                            }
                        }
                    });
                 }
            }
        });
    })(i);
} 

更新的一个仍未按预期工作,它返回一个空数组

var comment = [];
            for (var i = 0; i < jsons.length; i++){
                //var comments = [];
                var obj = jsons[i];
                /*if (typeof obj.IDNO === 'string' && obj.IDNO.length)
                {
                    alert('good');
                }
                else{
                    alert('bad');
                }*/
                //alert(obj.REASON);
                //console.log(obj);

                //alert(obj.REASON);


                if( i == jsons.length - 1 )
                {
                    alert(comment);
                    break;                      
                }
                var idno = '';
                idno = obj.IDNO;
                var amount = obj.AMOUNT;
                var jqxhr1 = $.ajax({
                    type: "POST",
                    url: "../include/salprefix.php",
                    data: {idnoauth: obj.IDNO, amount: amount, },
                    dataType: 'json',
                    });

                var jqxhr2 = $.ajax({
                    type: "POST",
                    url: "../include/salprefix.php",
                    data: {idnoinsert: obj.IDNO, amountinsert: amount},
                    });

                $.when(jqxhr1, jqxhr2).then(function(result, gg) {
                    //alert(result[0].idno + " " + result[0].amount);
                    if(result[0].idno == ''/*&& result[0].amount == '' && result[0].idnoerror != ''*/)
                    {
                        alert(result[0].idno + " " + result[0].amount);
                        comments.push(result);


                    }
                    else if(result[0].idno != ''/* && result[0].amount != '' && result[0].idnoerror == ''*/){
                        var idnoresult = result[0].idno;
                        var amountresult = result[0].amount;
                        if(gg != '')
                        {
                            comment.push(gg);
                            /*if( i == jsons.length - 1 )
                            {*/
                                //alert(gg);
                                //alert(comments);
                                //break;
                            //}
                        }
                    }

                    //alert(idnoresult + " " + amountresult);
                    // Handle both XHR objects
                    //alert("all complete");
                });
                //})(i);
            }enter code here

0 个答案:

没有答案