简体   繁体   中英

DataTables fails to load ajax data source objects

I want to do this: https://datatables.net/examples/ajax/objects.html So I have following response.php code:

<?php
include_once("info.php");
$sql = "SELECT * FROM table1";
$result = mysqli_query($link, $sql);
$amparray = array();
while($row = mysqli_fetch_assoc($result)){
    $emparray['data'][] = $row;
    }
header('Content-Type: application/json');
echo json_encode($emparray);
mysqli_close($link);
/*
The output will be of the form: 
{"data": [{"id":"1","Name":"Test1","URL":"test1.com","Language":"English","Number":"5165489"},{"id":"2","Name":"Test2","URL":"test2.com","Language":"English","Number":"4747489"}]}
*/
?>

and my index.php code:

<html>
<head>   
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
                   <script>
$(document).ready(function() {
    $('#example').DataTable( {        

                "ajax": {
                    "url": "response.php"
                },
        "columns": [
            { "data": "Name" },
            { "data": "URL" },
            { "data": "Number" },
            { "data": "Language" }
        ]
    } );
} );
</script>
    </head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th></th>
                <th>Name</th>
                <th>URL</th>
                <th>Number</th>
                <th>Language</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th></th>
                <th>Name</th>
                <th>URL</th>
                <th>Number</th>
                <th>Language</th>
            </tr>
        </tfoot>
    </table>    
    </body>
</html>

According to JSONLint my JSON output from response.php is valid and the same as wanted on datatables.net example. Even if I download the output and save as data.txt and use

"ajax": {"url": "data.txt"},

It still doesn't work. It just loads no data: No data avaiable in table.

*Edit in index.php HostName->Name //just a misstype in posting here-

Solved, the code I posted above now normally works. I have no idea why it didn't worked and now it works - probably some mistake like space was not space but some symbol - I just copied and pasted the code and now it works. Strange -.-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM