2017-07-26 149 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名字也是数据,并有一个缺少分号在同线;