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

时间:2014-05-16 04:14:48

标签: php jquery mysql ajax datatables

我正在尝试通过PHP和Linux从MySQL数据库中获取数据。使用DataTables在表中显示Ajax。我正在使用XAMPP 1.8.3

这是我的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>

这是我的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 ']';

这就是PHP脚本的输出数据的样子(现在有了新的更改):

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

这是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>

当我加载页面时(在Firefox中),我收到此错误: DataTables警告:table id = dataTables-melate - Ajax错误。
Firebug也说明了这个错误: 404 Not Found

我缺少什么? 很久以来我一直在努力解决这个问题:/

1 个答案:

答案 0 :(得分:3)

这个答案对于将AJAX与DataTables一起使用会有所不同,希望它能帮助一些人,因为它的代码要少得多。

当使用AJAX并向DataTables添加数据时,我通常采用以下方法: 1)在服务器端回显json_encode就像你正在做的那样。 2)在我的ajax调用的成功方法中我会有这个:

where&#34; column_data&#34;基本上只是一个对应的数据值数组 到每一栏。 DataTables通过计数自动添加数据 此数组中有多少个值并将每个值(列数据)推送到该行 基于数组中的索引。 所以基本上你只需要确保你拥有的列数 等于此数组的大小,并确保在此数组中,您的数据 按照您希望它显示的正确顺序。

$.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);
    }
});

因此,在PHP中,您并不需要将返回数据作为关联数组。这就是我目前正在实施它的方式,它对我来说很好。

注意:此方法的常见错误是返回数据数组的长度不等于您拥有的列数。所以要确保它们是平等的。如果不是,您可能会看到DataTables发出的错误,即暗示列不存在等等。