2017-07-26 92 views
0

我用一個AJAX調用從我的服務器獲取JSON信息加載表:無法獲取數據表與數據從JSON

console.log('GOT IT'); 
/* attach a submit handler to the form */ 
$('#search-form').submit(function(event) { 

    /* stop form from submitting normally */ 
    event.preventDefault(); 

    /* get the action attribute from the <form action=''> element */ 
    var form = $(this), 
    url = form.attr('action'); 


    var data = $('form#search-form').serialize() 

    /* Send the data using post with element id name and name2*/ 
    var posting = $.post(url, data, datype = 'json'); 

    /* Alerts the results */ 
    posting.done(function(data) { 
    $('#results-table').dataTable({ 
     ajax: data 
    }); 
    }); 
}); 

數據本身是從我的服務器在這種格式的JSON字符串返回:

{ 
    "success":true, 
    "result":[{"salutationtype":"Ms.","firstname":"Jennifer","contact_no":"CON1","phone":"","lastname":"Gale" 
} 

我想我可能不得不改變dataSrc到「結果」,但沒有奏效。我還試圖用$.jsonParse()JSON.parse()解析JSON,並將其作爲對象而不是JSON字符串提供給dataTables,但都無效。

+0

你在'data' var中得到了迴應? –

+0

嘗試用data.result更改數據 – gaetanoM

+0

試試這個。 '$( '#結果表')的dataTable({ aaData:數據 });' –

回答

0

請嘗試以下解決方案。這只是一個例子。

$('#results-table').dataTable({ 
         "bFilter": false, 
         "bRetrieve": false, 
         "bSortClasses": false, 
         "bLengthChange": true, 
         "bPaginate": true, 
         "bAutoWidth": true, 
         "aaSorting": [], 
         "aaData": data, 
         pagingType: "full_numbers", 
         scrollCollapse: false, 
         "sScrollX": true, 
         "autoWidth": true, 
         bAutoWidth: false, 
         responsive: true, 
         stateSave: true, 
         sDom: 'R<"data-table-wrapper"rt><"bottomside"ilp>', 
         "drawCallback": function(settings) { 
         }, 
         "aoColumns": [ 
          { 
           "sTitle": '<input type="checkbox" class="radio_select hathi">', 
           "mData": null, 
           "bSortable": false, 
           "render": function(obj) { 
            return '<input name="outcomes_chk" id="checkbox_class" type="checkbox" value="' + obj.id + '" class="radio_select madaniyu ">' 
           } 
          }, 
          { 
           "sTitle": "Name", 
           "mData": null, 
           "render": function(obj) { 
            return '<input type="text" data-name="name[]" class="required form-control input-sm font-body-size readonly transparent edit no-border" value="' + obj.name + '" >' 
           } 

          } 
         ] 
        }); 

aoColumns把你的專欄。

0

我建議你改變你的下面一行

var data = $('form#search-form').serialize() 

var formdata = $('form#search-form').serialize(); 
/* Send the data using post with element id name and name2*/ 
var posting = $.post(url, formdata, datype = 'json'); 

您變量名稱的數據和PARAM名字也是數據,並有一個缺少分號在同線;