簡體   English   中英

DataTables警告:table id = dataTables - Ajax錯誤。 404未找到

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

我試圖通過PHP和Ajax從MySQL數據庫中獲取數據,以便使用DataTables顯示在表中。 我正在使用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

我錯過了什么? 我這么久以來一直在努力:/

這個答案對於將AJAX與DataTables一起使用會有所不同,希望它能幫到一些,因為它的代碼要少得多。

當使用AJAX並向DataTables添加數據時,我通常會采用以下方法:1)在服務器端回顯json_encode就像你正在做的那樣。 2)在我的ajax調用的成功方法中,我會這樣:

其中“column_data”基本上只是與每列對應的數據值的數組。 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發出的錯誤,即暗示列不存在等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM