weixin_33688840 2017-01-03 07:23 采纳率: 0%
浏览 55

jQuery Ajax嵌套回调

I have nested callabcks but resulted output is not ordered correctly. My ajax results are in ascending orders of id but html generated is random. can someone help me out pls?

var formatedhtml = '';
$.ajax({
    type: "POST",
    url: BASE_URL + 'index.php/orders/read',
    dataType: 'json',
    success: function(data) {

        $.each(data, function(key, value) {

            console.log(value);
            getdetails(value['id'], function(output) {
                formatedhtml = formatedhtml +
                    '<div class="col-md-4 col-sm-4 col-lg-3 col-xs-6">' +
                    ' <div class="row">' +
                    ' <div class="orderno">' + value['id'] + '</div>' +
                    '<div class="tableno">' + value['tableno'] + '</div>' +
                    '<div class="ordertype">' + value['type'] + '</div>' +
                    '<div class="timestamp">' + value['created'] + '</div>' +
                    ' </div>' +
                    '<hr>';
                $.each(JSON.parse(output['items']), function(k, val) {
                    formatedhtml = formatedhtml + '<div class="row">' +
                        '<div class="quantity">' + val[3] + '</div>' +
                        '<div class="item">' + '</div>' +
                        '</div>';

                });
                formatedhtml = formatedhtml +
                    '<div class="row">' +
                    '<div class="notes">' + value['id'] + '</div>' +
                    '</div>' +
                    '</div>';
                $("#orderlist").html(formatedhtml);
                console.log(output);
            });

        });

    }
});

edit:

Here is getdetails function. its an ajax request.

function getdetails(id, callback) {
    var result;
    $.ajax({
        type: "POST",
        url: BASE_URL + 'index.php/orders/readdetails',
        dataType: 'json',
        data: {
            id: id,
        },
        success: function(data) {
            callback(data[0]);
        }
    });
};
  • 写回答

2条回答 默认 最新

  • ℙℕℤℝ 2017-01-03 07:40
    关注

    Use async:false in your getDetails() ajax call.

    function getdetails(id, callback) {
    var result;
    $.ajax({
        type: "POST",
        url: BASE_URL + 'index.php/orders/readdetails',
        dataType: 'json',
        async: false,
        data: {
            id: id,
        },
        success: function (data) {
            callback(data[0]);
    
    
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵