Can't get AJAX returns into a simple table (fairly noob to AJAX)
Can't get AJAX returns into a simple table (fairly noob to AJAX)
Hi there,
I've searched the forum and other sources but can't get the right answer anywhere.
I've got a simple table with temparture, date, time,zone and moisture readings.
The following code is inside the HTML JAva script (the HTML is as far as i can see correct but not included here to limit the post)
$(document).ready(function() {
$('#example').DataTable( {
"ajax": {
"url": "dag_test.php",
"dataSrc": ""
},
"columns": [
{ "data": "datum" },
{ "data": "tijd" },
{ "data": "zone" },
{ "data": "lucht" },
{ "data": "temperatuur" }
]
} );
} );
My php file:
```
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
$db = new PDO("sqlite:/home/pi/sensor.db");
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$result_array = array();
$result= array();
$query = "SELECT datum, tijd, zone, lucht, temperatuur FROM waarden WHERE datum = 20170626";
$vraag = $db->query($query);
foreach ($vraag as $row)
{
array_push($result_array,$row);
}
echo json_encode($result_array);
$db = null;
[{"datum":"20170626","tijd":"00:17","zone":"kelder","lucht":"54.6","temperatuur":"23.7"},{"datum":"20170626","tijd":"00:17","zone":"hal","lucht":"56.0","temperatuur":"22.6"}]
But somehow I can't get these readings in my table.
All I've got is a : 'TypeError: f is undefined[Learn More] jquery.dataTables.min.js:48:471" error
With some other code from this fine website I see that the responses returns 40 rows, so that seems to work fine.
Would somebody be so kind to help me?
Thank you
Maurice
This question has accepted answers - jump to:
Answers
Got it figured out...
Needed to add:
"dataType": "json",
Thx anyway.
This can be closed now
Maurice
Odd. DataTables does that by default. Good to hear you've got it working now though.
Allan
Got a strange setup...
Raspberry pi b+, Lighthttp server, php5.x & pdo, SQLite3 database. Bootstrap, jquery, python3, Pigpio, GPIO and all sorts of libraries that could mix things up.
I use a similar script without datatables, where I manually convert the json to html rows and there the explicit "dataType": "json", isn't needed.
No harm done though, it's better practice to make thing explicit...
Thanks for looking into this though....
Maurice
Nice setup .