简体   繁体   中英

DataTables Invalid JSON response v2

json data coming data coming from database javascript function

function fetch_data() {
  var dataTable = $('#Acc_data').DataTable({
    "bPaginate": false,
    "info": false,
    "processing": true,
    "serverSide": true,
    "order": [],
    "ajax": {
      url: "fetch_acc.php",
      type: "POST",
      "dataSrc": ""
    }
  });
}

HTML code

<table class="table" id="Acc_data" style="margin-left:30px;">
  <thead class=" text-primary text-center">
    <tr>
      <th>
        Account#
      </th>
      <th>
        Status
      </th>
      <th>
        Assigned_To
      </th>
      <th></th>
    </tr>
  </thead>
</table>

PHP code

while($row = mysqli_fetch_array($result))
{
 $sub_array = array();
 $sub_array[] = '<div contenteditable class="update text-center" data-id="'.$row["id"].'" data-column="Account#">' . $row["Accno"] . '</div>';
 $sub_array[] = '<div contenteditable class="update text-center" data-id="'.$row["id"].'" data-column="Status">' . $row["stat"] . '</div>';
 $sub_array[] ='<div contenteditable class="update text-center" data-id="'.$row["id"].'" data-column="Assigned_To">' . $row["email"] . '</div>';
 $sub_array[] = '<button type="button" name="delete" class="btn btn-danger btn-xs delete" id="'.$row["id"].'"><i class="fa fa-trash" aria-hidden="true"></i></button>';
 $data[] = $sub_array;
}   
 $output = array(
     "data"    => $data
    );    
    echo json_encode($output);

this is creating the error "DataTables warning: table id=Acc_data - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1 ". I have searched a lot but didn't find where I have done mistake. Any sort of help will be appreciated. Thanks in advance

I believe the error may be due to how you have structured the HTML. DataTables requires a specific layout for it to work:

<table id="table_id" class="display">
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Row 1 Data 1</td>
            <td>Row 1 Data 2</td>
        </tr>
        <tr>
            <td>Row 2 Data 1</td>
            <td>Row 2 Data 2</td>
        </tr>
    </tbody>
</table>

Above is taken from: https://datatables.net/manual/installation#HTML

So those <div> 's you have instead of <td> 's could be causing the issue.

Could you please also post a sample of the data returned from the query?

it was error in the query i wass passing . thanks every one for your kind response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM