AJAX(同步)内部循环的过程

时间:2018-09-05 09:59:41

标签: javascript jquery html ajax

在这种情况下如何执行同步?我有一个标头,将其拉出后,我将AJAX放入其中以拉出其详细信息并填充字段。

$.ajax({
  url: base_url + "order/get_order/" + id,
  type: "GET",
  dataType: "JSON",
  success: function(data) {
    var trankey = data.TranKey;
    $('[name="transactionkey"]').val(trankey);
    $('[name="qty"]').val(data.OrderQTY);
    loadStorer(1);
    loadSku(1);
    loadPNC(data.TruckerCode, data.PNC);
    $('[name="trucker"]').val(data.TruckerCode);
    $('[name="sku"]').val(data.sku);

    $.getJSON(base_url + "order/get_details/" + trankey, function(data) {
      if (data !== "") {
        fill_data(data);
      }
    });
  },
  async: false,
  error: function(jqXHR, textStatus, errorThrown) {
    alert('Error get data from ajax');
  }
});

function fill_data(data) {
  for (var i = 0; i >= data.length; i++) {
    if (i > 0) {
      add_row();
    }
    $('#store_code' + i).val(data[i]['StoreCode']);
    $('#sku' + i).val(data.data[i]['sku']);
    $('#ret_qty' + i).val(data[i]['ReturnedQty']);
    $('#shipped_qty' + i).val(data[i]['ShippedQty']);
  }
}

1 个答案:

答案 0 :(得分:0)

感谢所有评论。 .. 我只是想念我的for循环,一定是我

function fill_data(data) {
  for (var i = 0; i < data.length; i++) {
    if (i > 0) {
      add_row();
    }
    $('#store_code' + (i+1)).val(data[i]['StoreCode']);
    $('#sku' + (i+1)).val(data.data[i]['sku']);
    $('#ret_qty' + (i+1)).val(data[i]['ReturnedQty']);
    $('#shipped_qty' + (i+1)).val(data[i]['ShippedQty']);
  }
}

谢谢!