2017-04-26 19 views
0

對於我的生活,我不明白爲什麼會發生這種情況。數據回來,但無法發佈在jquery ajax

基本上我使用下面的代碼從數據庫中恢復錶行。

var getCposInputs = { 
      'type': 'viewcpos', 
      'quoteid': '<?php echo $_GET['id']; ?>' 
     }; 

     $.ajax({ 
      type: 'POST', 
      url: '/Applications/Controllers/Quotes/ajax-add-sin.php', 
      data: getCposInputs, 
      dataType: 'html', 
      encode: true 
     }).done(function(data){ 

      //$('body').html(data); 
      buildcotable += data; 

     }); 

因此,大家可以看到有被註釋掉行取出時給出了詳細的直入體,而不是稍後送出使用JavaScript中的的.html()函數的變量。

,所以該完整的jQuery代碼:

var buildcotable = ''; 
    var buildtrs = $('#formentry15').val(); 
    var coArray = ''; 
    var coArrayNumber = 1; 

    buildcotable += '<div class="table-responsive">'; 
    buildcotable += '<table class="table table-bordered">'; 

    buildcotable += '<thead>'; 
    buildcotable += '<th class="text-center">Number</th>'; 
    buildcotable += '<th class="text-center">Price</th>'; 
    buildcotable += '<th class="text-center">Installments</th>'; 
    buildcotable += '<th class="text-center">Contact Initials</th>'; 
    buildcotable += '<th class="text-center">Options</th>'; 
    buildcotable += '</thead>'; 

    buildcotable += '<tbody id="jquerypotable">'; 

    //lets do a check and see how many are listed 
    if(buildtrs != 'TBC'){ 

     var getCposInputs = { 
      'type': 'viewcpos', 
      'quoteid': '<?php echo $_GET['id']; ?>' 
     }; 

     $.ajax({ 
      type: 'POST', 
      url: '/Applications/Controllers/Quotes/ajax-add-sin.php', 
      data: getCposInputs, 
      dataType: 'html', 
      encode: true 
     }).done(function(data){ 

      $('body').html(data); 
      buildcotable += data; 

     }); 

    } else { 

     buildcotable += '<tr id="jqueryporow1">'; 
     buildcotable += '<td><input type="hidden" value="No" id="jqueryponumber1" class="form-control">No CPO\'s have been submitted</td>'; 
     buildcotable += '<td><input type="hidden" value="" id="jquerypovalue1" class="form-control"></td>'; 
     buildcotable += '<td class="text-center"><input type="hidden" value="" id="jquerypoinstallments1" class="form-control"></td>'; 
     buildcotable += '<td><input type="hidden" value="" id="jquerypocontactinitials1" class="form-control"></td>'; 
     buildcotable += '<td class="text-center">&nbsp;</td>'; 
     buildcotable += '</tr>'; 

    } 

    buildcotable += '</tbody>'; 

    buildcotable += '</table>'; 
    buildcotable += '<p><a href="#" class="btn btn-default btn-block" id="addnewpo">Add New CPO Number</a></p>'; 
    buildcotable += '<p><a href="#" class="btn btn-danger btn-block" id="ubldonepo">Done</a></p>'; 
    buildcotable += '</div>'; 

    $('.ubl-section-7').html(buildcotable); 

我知道數據是回來了罰款,因爲當我刪除了$(「身體」)的註釋HTML(數據)。然後顯示信息。

但是,如果試圖把它放入一個變量,它什麼也不做。

爲什麼會發生這種情況的任何想法?

感謝

+0

'完成'處理程序中'data'的值是什麼? –

+0

那麼,你的代碼在ajax請求完成之前呈現。所以基本上,在ajax請求檢索其數據之前,腳本的結束(很可能)已經到達。您需要一個同步解決方法。 – antesoles

+0

@RoryMcCrossan只是普通表信息。沒有什麼特別的,一組tr和td元素,其中包含數據。正如前面提到的,我知道由於在所提及的身體渲染中沒有錯誤。謝謝:) – Robert

回答

1

你必須確保Ajax請求檢索其數據後,輸出端產生。否則,即使在執行buildcotable += data;之前,也會運行最後一行代碼,因爲ajax請求尚未準備好(它是異步的)。嘗試這樣的:

var buildcotable_beg = '<table><tr> ...'; 
var buildcotable_cnt = ''; 
var buildcotable_end = '...</table>'; 
var full_bld_tbl = ''; 

if (buildtrs != 'TBC') { 
    $.ajax(...).done(function(buildcotable_cnt) { 
     full_bld_tbl = buildcotable_beg + buildcotable_cnt + buildcotable_end; 
     $('.ubl-section-7').html(full_bld_tbl); 
    }); 
} else { 
    buildcotable_cnt = '<tr>...</tr>'; 

    full_bld_tbl = buildcotable_beg + buildcotable_cnt + buildcotable_end; 
    $('.ubl-section-7').html(full_bld_tbl); 
}