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 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计