简体   繁体   English

DataTables 警告:表 - 无效的 JSON 响应

[英]DataTables warning: table - Invalid JSON response

I know this must be one of the most popular questions.我知道这一定是最受欢迎的问题之一。 I can't see what's wrong with my data though.我看不出我的数据有什么问题。 This is my php:这是我的 php:

  $sql = "SELECT eventid, event_type, date_display, date_from, date_to, location, done FROM events LIMIT 0, 30";

  $get_result = mysql_query($sql);

$arr = Array();
 while($res=mysql_fetch_assoc($get_result))
  {

              $arr['data'][] = $res;

  }

  echo json_encode($arr);

My JQuery:我的 JQuery:

function do_search()
{
 var search_term=$("#search_term").val();
 $.ajax
 ({
  type:'post',
  url:'get_results.php',
  data:{
   search:"search",
   search_term:search_term
  },
  success:function(response)
  {
     console.log(JSON.parse(response))

              $('#events').DataTable({
                ajax: JSON.parse(response),
                columns: [
                  { data: 'eventid' },
                  { data: 'event_type' },
                  { data: 'date_display' },
                  { data: 'date_from' },
                  { data: 'date_to' },
                  { data: 'location' },
                  { data: 'done' },
              ],
                "lengthMenu": [ [-1, 10, 25, 50, 100], ["All", 10, 25, 50, 100] ]
              });


  }
 });

And my HTML:还有我的 HTML:

<table id="events" class="display" style="width:100%">
          <thead>
              <tr>
                  <th>ID</th>
                  <th>Type</th>
                  <th>Date</th>
                  <th>From</th>
                  <th>To</th>
                  <th>Location</th>
                  <th>To do</th>

              </tr>
          </thead>

      </table>

This is the data I get in the console:这是我在控制台中获得的数据:

{
    "data": [
        {
            "eventid": "1",
            "event_type": "Senate meeting",
            "date_display": "61-60",
            "date_from": "61",
            "date_to": "60",
            "location": "Unknown",
            "done": "y"
        },
        {
            "eventid": "2",
            "event_type": "Legal hearing",
            "date_display": "73-70",
            "date_from": "73",
            "date_to": "70",
            "location": "Unknown",
            "done": ""
        },
[etc...]
    ]
}

As far as I can see I'm following the data expected by DataTable s.据我所知,我正在关注DataTable 期望的数据 What am I missing?我错过了什么?

I am getting seven fields for each record, and the table has seven fields indeed, also mapped in the JQuery code.我为每条记录获得七个字段,该表确实有七个字段,也映射到 JQuery 代码中。

In the example at the link you posted here: https://datatables.net/examples/ajax/objects.html , the ajax option in the datatables config is used to provide the URL of the file/script which returns the JSON data, so that datatables can initiate an AJAX request to fetch it.在您在此处发布的链接的示例中: https://datatables.net/examples/ajax/objects.html ,数据表配置中的ajax选项用于提供返回 JSON 数据的文件/脚本的 URL,这样数据表就可以发起 AJAX 请求来获取它。

Whereas what you've done is make your own AJAX request, and then pass the response to datatables.而您所做的是发出您自己的 AJAX 请求,然后将响应传递给数据表。 If you're going to do that, you should provide it to datatables via the data option instead.如果你打算这样做,你应该通过data选项将它提供给数据表。 Your scenario, as coded now, is actually closer to this example: https://datatables.net/examples/data_sources/js_array.html您现在编码的场景实际上更接近这个示例: https://datatables.net/examples/data_sources/js_array.html

eg例如

$('#events').DataTable({
  data: JSON.parse(response),

PS For a more complete discussion how to configure DataTables to use an AJAX request as a data source directly, see https://datatables.net/manual/ajax PS 有关如何配置 DataTables 以直接将 AJAX 请求用作数据源的更完整讨论,请参阅https://datatables.net/manual/ajax

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据表警告表 id=datatables-example - 无效的 json 响应 - Datatables warning table id=datatables-example - invalid json response DataTables警告:表格ID = staff_data-无效的JSON响应 - DataTables warning: table id=staff_data - Invalid JSON response DataTables警告:表格ID = {id}-无效的JSON响应 - DataTables warning: table id={id} - Invalid JSON response DataTables警告:table id = example - 无效的JSON响应 - DataTables warning: table id=example - Invalid JSON response php DataTables中的Ajax警告:表格ID = example-无效的JSON响应 - Ajax from php DataTables warning:table id=example - Invalid JSON response DataTables 警告:表 id=tbl - 无效的 JSON 响应。 关于这个错误,请参阅 http://datatables.net/tn/1 - DataTables warning: table id=tbl - Invalid JSON response. about this error, please see http://datatables.net/tn/1 “数据表警告:无效的JSON响应”和“错误:找不到图容器元素” - “DataTables warning: Invalid JSON response” and “Error: Graph container element not found” 数据表中的JSON响应无效 - Invalid JSON Response in Datatables 数据表警告:表 id=table_companies - JSON 响应无效。 有关此错误的更多信息,请参阅 http://datatables.net/tn/1 - DataTables warning: table id=table_companies - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1 DataTables警告:Json响应不起作用 - DataTables warning: Json Response not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM