每个循环多个ajax php请求

时间:2016-06-14 19:45:59

标签: php jquery ajax request each

我有这个代码: 它来自一些如果查询,这是完美的。 第一个ajax请求也可以正常工作但在每个循环之后第二个请求不会被执行。 Idk为什么,希望有人有解决方案。

                        alert("success");               // Everything is OK 
                        $( ".nrlist-create_item-select" ).each(function() {
                            var item_id = $( this ).attr('id');
                            var item_name = $( this ).attr('title');

                            $.ajax({
                                type: "POST",
                                url: "games/create-nr_vs_nr-update.php",
                                datatype: "text",
                                data: {item_id: item_id, item_name: item_name, nrlistcreatenumber: nrlistcreatenumber, nrlistcreatetime: nrlistcreatetime},
                                success: function(data) {
                                    alert(data);
                                    requestCallback.requestComplete(true);
                                }
                            });

                            return true;
                        });

                        $.ajax({
                            type: "POST",
                            url: "games/create-nr_vs_nr-insert.php",
                            datatype: "text",
                            data: {item_id: item_id, item_name: item_name, nrlistcreatenumber: nrlistcreatenumber, nrlistcreatetime: nrlistcreatetime},
                            success: function(data) {
                                alert(data);
                                requestCallback.requestComplete(true);
                            }
                        });

2 个答案:

答案 0 :(得分:3)

var item_id;
var item_name;

似乎没有为第二个ajax调用定义这些变量。并且请记住,javascript是异步的,它不会等待你的第一个ajax调用在它调用第二个之前完成。如果您需要这样做,请查看一个jquery deffered对象。

答案 1 :(得分:0)

你必须创建一个这样的循环函数:

$ajaxsend=function(data,$arrayitems,url,total,ini){
    $.ajax({
        type: "POST",
        url: url,
        datatype: "text",
        data: data,
        success: function(data) {
            console.log(data);
            //requestCallback.requestComplete(true);
            //ini increment each loop
            ini+=1;
            //validate loop while total is > than ini
            if(total > ini){
                data = {item_id: $arrayitems[ini].item_id, item_name: $arrayitems[ini].item_name, nrlistcreatenumber: nrlistcreatenumber, nrlistcreatetime: nrlistcreatetime};
                //resend ajax call
                $ajaxsend(data,$arrayitems,url,total,ini);
            }
        }
    });
};
var $arrayitems = [];
$( ".nrlist-create_item-select" ).each(function() {
    var item_id = $( this ).attr('id');
    var item_name = $( this ).attr('title');
    //save items in array
    $arrayitems.push({"item_id":item_id,"item_name":item_name});
    //return true;
});
//initial
var ini = 0;
//total items
var total = $arrayitems.length;
//data
var data = {item_id: $arrayitems[ini].item_id, item_name: $arrayitems[ini].item_name, nrlistcreatenumber: nrlistcreatenumber, nrlistcreatetime: nrlistcreatetime};
//ajax for loop function
$ajaxsend(data,$arrayitems,'games/create-nr_vs_nr-update.php',total,ini);