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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥30 如何降低hdfs中datanode的JVM内存用量
      • ¥15 Android URL如何转成视频/音频,可行吗?
      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 关于#sql#的问题:没有用命令关闭cdc,手动把系统表开启的cdc右键删除了
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?