无法使用JSON加载数据表

时间:2023-01-11 14:25:55

I am trying to load JSON into a jQuery data table.

我正在尝试将JSON加载到jQuery数据表中。

When firing the change event, using the below method, I can return to the console the exact same data I need to load in the data table:

当触发变更事件时,使用下面的方法,我可以返回到控制台,我需要在数据表中加载相同的数据:

 $('#serviceload').change(function()
 {
   var page = $('#serviceload').val();

   $.ajax({
     "type": 'POST',
     "url": 'api/service_teus.php',
     "data": {page:page},
     //"dataType": 'json',
     "success": function(data){
        console.log('success', data);
     },
     "error": function(msg){
        console.log('fail', msg);
     }
   });
 });

Now when I change the code above to include the data table, this is where I'm stuck:

现在,当我将上面的代码更改为包含数据表时,这就是我被卡住的地方:

 $('#serviceload').change(function()
 {
   var page = $('#serviceload').val();

   var $dataTable = $('#example1').DataTable({
     "ajax":{
       "url": 'api/service_teus.php',
       "data": {page:page},
       "dataType": 'json',
       "success": function(data){
         console.log('success', data);
        },
       "error": function(msg){
         console.log('fail', msg);
        }
      }
 });

Using the above method, the console returns an array of objects, but doesn't load the data table.

使用上述方法,控制台将返回一个对象数组,但不加载数据表。

Answering this question will actually help me answer another question I posted.

回答这个问题实际上可以帮助我回答我贴出的另一个问题。

Thank you for all your help, fellow coders.

谢谢大家的帮助,程序员们。

****** EDIT ******

* * * * * * * * * * * *进行编辑

In LeeLuu voice: "Please......help..."

在LeeLuu声音:“请……帮助……”

2 个解决方案

#1


3  

Your dataTable is likely being generated with the ajax promise and not the returned data. Generate the datatable inside your success function instead of using the entire ajax statement.

您的dataTable很可能是使用ajax承诺而不是返回数据生成的。在success函数中生成datatable,而不是使用整个ajax语句。

Something like this.

是这样的。

$('#serviceload').change(function()
 {
   var page = $('#serviceload').val();

   $.ajax({
     "type": 'POST',
     "url": 'api/service_teus.php',
     "data": {page:page},
     "dataType": 'json',
     "success": function(data){
        console.log('success', data);
        $dataTable = $('#example1').DataTable(data);
     },
     "error": function(msg){
        console.log('fail', msg);
     }
   });
 });

#2


0  

I used my answer from this page: Load JQUERY DATATABLE on CHANGE event

我使用了这个页面中的答案:在CHANGE event上加载JQUERY DATATABLE

And I was able to finally move on.

我终于可以继续前进了。

#1


3  

Your dataTable is likely being generated with the ajax promise and not the returned data. Generate the datatable inside your success function instead of using the entire ajax statement.

您的dataTable很可能是使用ajax承诺而不是返回数据生成的。在success函数中生成datatable,而不是使用整个ajax语句。

Something like this.

是这样的。

$('#serviceload').change(function()
 {
   var page = $('#serviceload').val();

   $.ajax({
     "type": 'POST',
     "url": 'api/service_teus.php',
     "data": {page:page},
     "dataType": 'json',
     "success": function(data){
        console.log('success', data);
        $dataTable = $('#example1').DataTable(data);
     },
     "error": function(msg){
        console.log('fail', msg);
     }
   });
 });

#2


0  

I used my answer from this page: Load JQUERY DATATABLE on CHANGE event

我使用了这个页面中的答案:在CHANGE event上加载JQUERY DATATABLE

And I was able to finally move on.

我终于可以继续前进了。