php 从 DataTables 中第 0 行的数据源请求未知参数“1”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12280513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:12:50  来源:igfitidea点击:

Requested unknown parameter '1' from the data source for row 0 in DataTables

phpcodeigniter-2jquery-datatables

提问by LiveEn

When I try to retrieve data from my database to the table, I get this error:

当我尝试将数据从我的数据库检索到表时,我收到此错误:

DataTables warning (table id = 'student_table'): Requested unknown 
parameter '1' from the data source for row 0

Below is the javascript that I used

下面是我使用的javascript

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
    $('#student_table').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sServerMethod": "POST",
        "sAjaxSource": "<?php echo base_url()?>index.php/data/all"
    } );            
} );
</script>

The JSON data retrieved:

检索到的 JSON 数据:

{"sEcho":0,"iTotalRecords":3,
"iTotalDisplayRecords":3,
"aaData":[["85","t1","1D"],["74","test475","4A"],
["777","maiz","5"]],"sColumns":"id,name,class"}

Below is my table:

下面是我的表:

<table class="datatable tables" id="student_table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Class</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="dataTables_empty">Loading data from server</td>
        </tr>
    </tbody>
</table> 

PHP code (ignited datatables)

PHP 代码(点燃的数据表)

$this->load->library('datatables');

$this->datatables->select('admission,name,class');
$this->datatables->from('students');
echo $this->datatables->generate();

I'm using codeigniter and DataTables.

我正在使用 codeigniter 和 DataTables。

Why am I getting that error and how to retrieve the data to the table?

为什么我会收到该错误以及如何将数据检索到表中?

回答by Sachin Prasad

I also had the same issue. The issue is here:

我也有同样的问题。问题在这里:

<tr>
          <td class="dataTables_empty">Loading data from server</td>
</tr>

You have three <TH>but only one <td>Adding two more <td>will fix your error. One more thing,If no data is available It will display the message automatically you need not display the message.In this case you can remove this as it will be done automatically:

您有三个,<TH>但只有一个 再<td>添加两个<td>将修复您的错误。还有一件事,如果没有可用数据,它将自动显示消息,您不需要显示消息。在这种情况下,您可以删除它,因为它会自动完成:

<tr>
          <td class="dataTables_empty">Loading data from server</td>
</tr>

In order to customize the message pass this as an option "sEmptyTable": "Loading data from server"

为了自定义消息,将此作为选项传递 "sEmptyTable": "Loading data from server"

$('.datatable ).dataTable({
  "bFilter": false,
   "bPaginate": false,
   "bLengthChange": false,
   "bInfo": false,
   "oLanguage": {
    "sEmptyTable": '',
    "sInfoEmpty": ''
   },
   "sEmptyTable": "Loading data from server"
 });

回答by JvdBerg

You are using the POST method to get the data. If you follow php the example that is provided with datatables, the GET method is used. I assume that when you use sorting or searching all the requests are GETs.

您正在使用 POST 方法来获取数据。如果您遵循 php 随datatables提供的示例,则使用GET 方法。我假设当您使用排序或搜索时,所有请求都是 GET。

回答by MemeDeveloper

Couple of ideas which might help...

一些想法可能会有所帮助......

  1. Make sure your response from server is correctly formatted JSON, with correct header. e.g. https://stackoverflow.com/a/4064468/661584not sure about igniter / php myself but may be an issue.

  2. Not sure the sColumns param is correct there on its own, think that's for reordering of cols on client... and only used with sName see http://datatables.net/usage/columns#sNameand http://datatables.net/usage/server-side

  1. 确保来自服务器的响应格式正确,JSON 格式正确,标头正确。例如 https://stackoverflow.com/a/4064468/661584我自己不确定点火器/php,但可能是一个问题。

  2. 不确定 sColumns 参数本身是否正确,认为这是为了重新排序客户端上的列......并且仅与 sName 一起使用,请参阅 http://datatables.net/usage/columns#sNamehttp://datatables.net /用法/服务器端

So that might be messing it up.

所以这可能会搞砸。

  1. If its something else, maybe look at answer here... might be some help.
  1. 如果是别的东西,也许看看这里的答案......可能会有所帮助。

Good luck

祝你好运

回答by klabrams

We were having similar issues...

我们遇到了类似的问题...

But, before you go nuts - check the data in the tables. In our case, our data had hyperlinks and curly quotes used in the data that populated the table - those quotes curly quotes were getting stripped out when the data was uploaded from a CSV file. Short story is that IE couldn't deal with it, but Chrome and Firefox ignored it.

但是,在你发疯之前 - 检查表中的数据。在我们的例子中,我们的数据在填充表格的数据中使用了超链接和弯引号——当从 CSV 文件上传数据时,这些引号被去掉了。简而言之,IE 无法处理它,但 Chrome 和 Firefox 忽略了它。