数据表警告:表中的id =数据表 - 阿贾克斯错误。 404未找​​到 [英] DataTables warning: table id=dataTables - Ajax error. 404 Not Found

查看:564
本文介绍了数据表警告:表中的id =数据表 - 阿贾克斯错误。 404未找​​到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过PHP和放大器来获得一个MySQL数据库中的数据; Ajax来通过使用数据表被显示在表中。我使用的XAMPP 1.8.3

I am trying to get data from a MySQL database through PHP & Ajax to be displayed in a table by using DataTables. I am using XAMPP 1.8.3

这是我的html code部分:

This is part of my html code:

<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脚本(现在编辑和工作):

This is my php script (now edited and working):

    //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脚本的输出数据看起来像(现在有了新的变化):

This is how the output data of the PHP script looks like (now with the new change):

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

这里是jQuery的code:

And here is the jQuery code:

<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),我得到这个错误: 的DataTable警告:表中的id =数据表,melate - 阿贾克斯误差
萤火虫告诉有这样的错误,以及: 404未找​​到

When i load the page (in Firefox), I get this error: DataTables warning: table id=dataTables-melate - Ajax error.
Firebug tells there's this error as well: 404 Not Found

我在想什么? 我一直在努力奋斗着这个,因为这么长时间:/

What am i missing? I've been struggling with this since so long :/

推荐答案

这个答案将是有所不同的方法来使用Ajax和数据表,并希望这将帮助一些,因为它少得多code

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和将数据添加到数据表,我通常走这条路: 1)呼应json_en在服务器端code就像你在做什么。 2)在我的AJAX调用的成功方法,我想有这样的:

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. 2) in the success method of my ajax call I would have this:

在哪里COLUMN_DATA基本上是对应的数据值的只是一个数组 到每一列。数据表这种方法通过计算自动将数据 多少个值都在此数组和推每个值(列数据),以行 基于索引的阵列中。 所以基本上你只需要只是为了确保列你有多少 等于该数组的大小,同时也确保在这个数组,你的数据 是按正确的顺序,你希望它显示出来。

Where "column_data" is basically just an array of the data values that correspond to each column. 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. 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);
    }
});

所以,在PHP中,你并不真的需要返回的数据是一个关联数组。这是如何我目前正在实施它,它为我工作正常。

So in PHP you don't really need the return data to be an associative array. 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.

这篇关于数据表警告:表中的id =数据表 - 阿贾克斯错误。 404未找​​到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆