Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

javascript - DataTables无效的JSON响应v2(DataTables Invalid JSON response v2)

json data coming data coming from database javascript function

(json数据 来自数据库 javascript函数的数据)

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

(HTML代码)

<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

(PHP代码)

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 ".

(这将导致错误“ DataTables警告:表id = Acc_data-无效的JSON响应。有关此错误的更多信息,请参见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

(提前致谢)

  ask by user translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe the error may be due to how you have structured the HTML.

(我相信该错误可能是由于您如何组织HTML。)

DataTables requires a specific layout for it to work:

(DataTables需要特定的布局才能工作:)

<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

(以上摘录自: https : //datatables.net/manual/installation#HTML)

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

(因此,您拥有的那些<div>而不是<td>可能是造成此问题的原因。)

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

(您还可以发布查询返回的数据样本吗?)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...