简体   繁体   English

DataTables警告:table id = dataTables - Ajax错误。 404未找到

[英]DataTables warning: table id=dataTables - Ajax error. 404 Not Found

I am trying to get data from a MySQL database through PHP & Ajax to be displayed in a table by using DataTables. 我试图通过PHP和Ajax从MySQL数据库中获取数据,以便使用DataTables显示在表中。 I am using XAMPP 1.8.3 我正在使用XAMPP 1.8.3

This is part of my html code: 这是我的HTML代码的一部分:

<table id="dataTables-melate" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                                <thead>
                                    <tr>
                                        <th>Concurso</th>
                                        <th>R1</th>
                                        <th>R2</th>
                                        <th>R3</th>
                                        <th>R4</th>
                                        <th>R5</th>
                                        <th>R6</th>
                                    </tr>
                                </thead>

                                <tfoot>
                                    <tr>
                                        <th>Concurso</th>
                                        <th>R1</th>
                                        <th>R2</th>
                                        <th>R3</th>
                                        <th>R4</th>
                                        <th>R5</th>
                                        <th>R6</th>
                                    </tr>
                                </tfoot>
</table>

This is my php script ( now edited and working ): 这是我的PHP脚本( 现在编辑和工作 ):

    //default_chart_numbers.php
    $loteria='revancha';
    $lotto = new Lotto();

    $ultimos_resultados=$lotto->last_results($loteria,20);

    //echo json_encode($ultimos_resultados);
/*Formatting the output to a non associative array*/
function objectToArray($d) 
{
    if (is_object($d)) {
        // Gets the properties of the given object
        // with get_object_vars function
        $d = get_object_vars($d);
    }

    if (is_array($d)) {
        /*
        * Return array converted to object
        * Using __FUNCTION__ (Magic constant)
        * for recursive call
        */
        return array_map(__FUNCTION__, $d);
    } else {
        // Return array
        return $d;
    }
}

$new_array = objectToArray($ultimos_resultados);
//echo '<pre>',print_r($new_array),'</pre>';

$result = array();
echo '[';
foreach ($new_array as $new_array2) {
    echo '[';
    foreach ($new_array2 AS $value){
        echo $value;
        if($value!==end($new_array2)){ //referencias: http://stackoverflow.com/a/8780881/1883256
            echo',';
        }
    }
    echo ']';//referencias: http://www.mydigitallife.info/how-to-access-php-array-and-multidimensional-nested-arrays-code-syntax/
    if($new_array2!==end($new_array)){
        echo ',';
    }else{ echo '';}
}
echo ']';

This is how the output data of the PHP script looks like ( now with the new change ): 这就是PHP脚本的输出数据的样子( 现在有了新的更改 ):

[[2738,11,12,28,30,50,54], ... ,[2757,32,34,35,36,50,55]]

And here is the jQuery code: 这是jQuery代码:

<script>
$(document).ready(function() {
    $('#dataTables-melate').dataTable({
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": {
            "url":"ajax/default_chart_numbers.php",
            "type": "POST"
        },
        "columns":[
            {"data": "concurso"},
            {"data": "R1"},
            {"data": "R2"},
            {"data": "R3"},
            {"data": "R4"},
            {"data": "R5"},
            {"data": "R6"}
        ]
    });
} );
</script>

When i load the page (in Firefox), I get this error: DataTables warning: table id=dataTables-melate - Ajax error. 当我加载页面(在Firefox中)时,我收到此错误: DataTables警告:table id = dataTables-melate - Ajax错误。
Firebug tells there's this error as well: 404 Not Found Firebug也告诉我这个错误: 404 Not Found

What am i missing? 我错过了什么? I've been struggling with this since so long :/ 我这么久以来一直在努力:/

This answer is going to be a somewhat different approach to using AJAX with DataTables, and hopefully it will help some, because it's much less code. 这个答案对于将AJAX与DataTables一起使用会有所不同,希望它能帮到一些,因为它的代码要少得多。

When using AJAX and adding data to DataTables I usually go this route: 1) echo json_encode on the server side just like you are doing. 当使用AJAX并向DataTables添加数据时,我通常会采用以下方法:1)在服务器端回显json_encode就像你正在做的那样。 2) in the success method of my ajax call I would have this: 2)在我的ajax调用的成功方法中,我会这样:

Where "column_data" is basically just an array of the data values that correspond to each column. 其中“column_data”基本上只是与每列对应的数据值的数组。 DataTables automatically adds data this way by counting how many values are in this array and pushing each value (column data) to the row based on the index in the array. DataTables通过计算此数组中有多少值并根据数组中的索引将每个值(列数据)推送到行来自动添加数据。 So basically you just need just to make sure the number of columns you have equals the size of this array, and also make sure that in this array, your data is in the correct order that you want it to be displayed. 所以基本上你只需要确保你拥有的列数等于这个数组的大小,并确保在这个数组中,你的数据的顺序是你希望它显示的正确顺序。

$.ajax({
    url: "your_path",
    type: "post_or_get",
    success : function (resp){
        // would look something like ['val1','val2', 'etc']
        var column_data = $.parseJSON(resp);

        // adding data to datatables
        // if column_data is 1 row
        $('your_table_element').dataTable().fnAddData(column_data);

        // to add multiple rows (array of arrays, just loop)
        for (var j=0;j<=column_data.length-1;++j){
            // adding each row with its column data
            $('your_table_element').dataTable().fnAddData(column_data[j]);
        }
    },
    error: function(jqXHR, textStatus, ex) {
      console.log(textStatus + "," + ex + "," + jqXHR.responseText);
    }
});

So in PHP you don't really need the return data to be an associative array. 因此在PHP中,您并不需要将返回数据作为关联数组。 This is how I'm currently implementing it and it works fine for me. 这就是我目前正在实现的方式,它对我来说很好。

Note: A common error with this method is the length of the return data array not equaling the number of columns you have. 注意:此方法的常见错误是返回数据数组的长度不等于您拥有的列数。 So make sure they are equal. 所以要确保它们是平等的。 If they are not, you'll probably see an error from DataTables saying hinting that a column doesn't exist etc, etc. 如果不是,您可能会看到DataTables发出的错误,即暗示列不存在等等。

暂无
暂无

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

相关问题 DataTables 警告:表 id=datatable - Ajax 错误。 使用 laravel - DataTables warning: table id=datatable - Ajax error. Using laravel DataTables警告:表格ID = DataTables_Table_0-Ajax错误 - DataTables warning: table id=DataTables_Table_0 - Ajax error DataTables 警告:table id=table - Ajax 错误。 有关此错误的更多信息,请参阅 http://datatables.net/tn/7 - DataTables warning: table id=table - Ajax error. For more information about this error, please see http://datatables.net/tn/7 DataTables 警告:表 id=products - Ajax 错误。 有关此错误的更多信息,请参阅 http://datatables.net/tn/7 - DataTables warning: table id=products - Ajax error. For more information about this error, please see http://datatables.net/tn/7 DataTables警告:表id = table-timsheet-Ajax错误。 有关此错误的更多信息,请参见http://datatables.net/tn/7 - DataTables warning: table id=table-timsheet - Ajax error. For more information about this error, please see http://datatables.net/tn/7 错误:DataTables警告:表id = example-使用DataTables服务器端AJAX请求的未知参数 - Error: DataTables warning: table id=example - Requested unknown parameter using DataTables Server-side AJAX 数据表警告表 id=datatables-example - 无效的 json 响应 - Datatables warning table id=datatables-example - invalid json response 数据表,数据库服务器端处理和“数据表警告(表ID =&#39;示例&#39;)” - DataTables, database serverside processing and “DataTables warning (table id = 'example')” jQuery datatables mysql php DataTables警告(表id =&#39;displayData&#39;):DataTables警告:无法解析JSON数据 - jquery datatables mysql php DataTables warning (table id='displayData'): DataTables warning: JSON data from could not be parsed php DataTables中的Ajax警告:表格ID = example-无效的JSON响应 - Ajax from php DataTables warning:table id=example - Invalid JSON response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM