简体   繁体   English

DataTables无法加载Ajax数据源对象

[英]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: 我想这样做: https : //datatables.net/examples/ajax/objects.html所以我有以下response.php代码:

<?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: 和我的index.php代码:

<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. 根据JSONLint,我来自response.php的JSON输出是有效的,并且与datatables.net示例中所需的相同。 Even if I download the output and save as data.txt and use 即使我下载输出并另存为data.txt并使用

"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- *在index.php HostName-> Name中编辑//在此处发布时只是一个错误类型-

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 -.- 奇怪-.-

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

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