I am trying to construct a data table to display the result of an ajax database call. The JSON object that is returned is correct, and looks like this:
[{"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}]
However the data table will not display anything because of a Type Error:
d is undefined
error message in the console. The line number is jquery.dataTables.min.js:62:257 but I have no idea how to read that file and I can't exactly figure out what that means or where the problem is.
Here is the table on the index.php page:
<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>
This is the Scripts.js file which holds the ajax call:
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()
And lastly here is the API.php file which has the actual database query:
$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()) . '}';
EDIT::
I found that the issue was that the data the server responded to the ajax call with was not a proper json object. So I entered what it did respond with into JSONLint and found this:
{
"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
}]
}
With Error message:
Error: Parse error on line 2:
{ "data" [{ "prof_id": "1",
---------^
Expecting 'EOF', '}', ':', ',', ']', got '['
But I am not exactly sure how to remedy this problem. Any advice would be great.