Bootstrap数据表类型错误:d未定义

时间:2019-12-17 01:26:09

标签: php json sql-server ajax datatables

我正在尝试构造一个数据表以显示ajax数据库调用的结果。返回的JSON对象是正确的,看起来像这样:

[{"prof_id":"1","0":"1","prof_name":"Cole Test1","1":"Cole Test1","prof_SQL":"JUST SQL JAJA","2":"JUST SQL JAJA","date":null,"3":null},{"prof_id":"2","0":"2","prof_name":"Doobie Doobie","1":"Doobie Doobie","prof_SQL":"my SQL statement to be executed","2":"my SQL statement to be executed","date":null,"3":null},{"prof_id":"3","0":"3","prof_name":"Cole Test 2","1":"Cole Test 2","prof_SQL":"my SQL statement to be executed that is better than the last","2":"my SQL statement to be executed that is better than the last","date":null,"3":null}]

但是由于类型错误,数据表将不显示任何内容:

  

d未定义

控制台中的

错误消息。行号是jquery.dataTables.min.js:62:257,但我不知道如何读取该文件,而且我无法确切弄清楚这意味着什么或问题出在哪里。

这是index.php页面上的表:

 <div>
    <table id="example" class="display" width="100%">
        <thead>
            <tr>
                <th>
                    prof_id
                </th>
                <th>
                    prof_name
                </th>
            </tr>
        </thead>    
    </table>
    </div>

    <script src="includes/Scripts.js"></script>

这是保存ajax调用的Scripts.js文件:

function fetchQueries(){
       $.ajax({
            type: "GET",
            url: "API.php",
            datatype: "json"  
        }).done(function(returnresult) {
           console.log(returnresult);
           $(".query-div").text(returnresult);

           $('#example').DataTable( {
       "ajax": "API.php",
        "columns": [
            { "data": "prof_id" },
            { "data": "prof_name" }
        ]
    } );
        })
}

fetchQueries()

最后是具有实际数据库查询的API.php文件:

$object = new Dbh; //Create insteance of Dbh
$object->connect(); //Call the connect function to connect to db

$pdo = $object->getPdo(); //Call getPdo from Dbh to get an instance of the pdo

$prof_Query = $pdo->query('SELECT * FROM oogns_quries'); //Creating the db query 
$prof_Query->execute();
echo '{"data"' . json_encode($prof_Query->fetchAll()) . '}';

编辑::

我发现问题在于服务器响应ajax调用的数据不是正确的json对象。因此,我将它的响应内容输入JSONLint并找到了:

{
    "data" [{
        "prof_id": "1",
        "0": "1",
        "prof_name": "Cole Test1",
        "1": "Cole Test1",
        "prof_SQL": "JUST SQL JAJA",
        "2": "JUST SQL JAJA",
        "date": null,
        "3": null
    }, {
        "prof_id": "2",
        "0": "2",
        "prof_name": "Doobie Doobie",
        "1": "Doobie Doobie",
        "prof_SQL": "my SQL statement to be executed",
        "2": "my SQL statement to be executed",
        "date": null,
        "3": null
    }, {
        "prof_id": "3",
        "0": "3",
        "prof_name": "Cole Test 2",
        "1": "Cole Test 2",
        "prof_SQL": "my SQL statement to be executed that is better than the last",
        "2": "my SQL statement to be executed that is better than the last",
        "date": null,
        "3": null
    }]
}

出现错误消息:

Error: Parse error on line 2:
{   "data" [{       "prof_id": "1",
---------^
Expecting 'EOF', '}', ':', ',', ']', got '['

但是我不确定如何解决此问题。任何建议都很好。

1 个答案:

答案 0 :(得分:1)

尝试像这样回显您的结果:

echo '{"data":' . json_encode($prof_Query->fetchAll()) . '}';

来源https://datatables.net/examples/ajax/objects.html(单击“ Ajax”标签)