du521521521 2016-07-17 14:55
浏览 19
已采纳

在执行下一个ajax之前执行ajax inside condition

I have two tables in a database , when pressing the submit button I want to insert new trader into trader table and get id using laravel 5.2 by using post ajax under condition ,then execute another post ajax for invoice insert , but the problem in this case is when I press the submit it execute second ajax before the first ajax .. why?

$(document).ready(function(e) {

$('#invoiceForm').validate({

    //rules: itemRules,

    errorPlacement: function (element) {
        return false;
    },
    submitHandler: function (event) {
        //event.preventDefault();
       // $('#selectedTraderName').valid();
        var allItems = [];
        $('tr.inputsItem').each(function () {
         rowData = $(this).children('td').find('.form-control').map(function () {
            return $(this).val();
        }).get();
            rowData.push(parseInt($(this).children('td').find('strong#autoTotal').text().trim()));
            if($('select[id=selectedTraderName]').val() <= lastTraderID) {
                rowData.push($('select[id=selectedTraderName]').val());
            }
            else{
                newTrader = [];
                newTrader.push($('#name').val());
                newTrader.push($('#traderMail').val());
                newTrader.push($('#mobileNumber').val());
                newTrader.push($('#address').val());
                $.ajax({
                    type: 'POST',
                    url: insertNewTrader,
                    data:{_token:token, trader:newTrader}
                }).done(function (msg) {
                    rowData.push(msg['id']);
                });

            }
            alert(rowData[6]);
            allItems.push(rowData);
            $.ajax({
                type: 'POST',
                url: insertNewItem,
                data:{_token:token, items:allItems}
            }).done(function () {
                alert('done');
            });
        });


    }

});
  • 写回答

2条回答 默认 最新

  • dtmjl4427 2016-07-17 15:11
    关注

    Of course, you can't nest ajax calls because of the if/else statement instead you can use a Deferred object like this:

    rowData.push(parseInt($(this).children('td').find('strong#autoTotal').text().trim()));
    // Create a new Deferred object
    var deferred = $.Deferred();
    if ($('select[id=selectedTraderName]').val() <= lastTraderID) {
        rowData.push($('select[id=selectedTraderName]').val());
        //Nothing to wait just resolve the deferred
        deferred.resolve();
    } else {
        newTrader = [];
        newTrader.push($('#name').val());
        newTrader.push($('#traderMail').val());
        newTrader.push($('#mobileNumber').val());
        newTrader.push($('#address').val());
        $.ajax({
            type: 'POST',
            url: insertNewTrader,
            data: {
                _token: token,
                trader: newTrader
            }
        }).done(function(msg) {
            rowData.push(msg['id']);
            //Resolve the deferred
            deferred.resolve();
        });
    
    }
    //When the deferred resolved then execute your next ajax request
    deferred.then(function() {
        allItems.push(rowData);
        $.ajax({
            type: 'POST',
            url: insertNewItem,
            data: {
                _token: token,
                items: allItems
            }
        }).done(function() {
            alert('done');
        });
    });
    

    I hope thsi will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条