DataTable Warning: table id='dataTables-example' -Requested unknown parameter for row 0, column 0
DataTable Warning: table id='dataTables-example' -Requested unknown parameter for row 0, column 0
cn94
Posts: 2Questions: 1Answers: 0
I keep getting this error and though the right number of entries is listed in the table but no data is displayed. i have the correct amount of columns as in my table and my JSON is valid. I'm using node JS as my to query my database and return the results as JSON. Also the debugger crashes every time I try to run it on my page.
AJAX
$(document).ready(function() {
$('#dataTables-example').DataTable( {
"serverSide": false,
"processing": true,
"ajax":{
"url":"http://localhost:8888/data",
"dataType": "JSON",
"dataSrc": "",
"aLengthMenu": "25",
"columns": [
{ "data": "Name" },
{ "data": "Address"},
{ "data": "Email" },
{ "data": "Phone" },
{ "data": "Fax"},
{ "data": "Job" },
{ "data": "University" },
{ "data": "City" },
{ "data": "State" },
{ "data": "Zip" },
{ "data": "Country" },
{ "data": "Status" }
]
}
});
});
HTML
<table class="table table-striped table-bordered table-hover" id="dataTables-example" >
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Email</th>
<th>Phone</th>
<th>Fax</th>
<th>Job</th>
<th>University</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Country</th>
<th>Status</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Node JS
var mysql = require("mysql");
var express = require('express');
var app = express();
//Create connection to the db
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
database: "records"
});
app.get('/data', function(req, res) {
res.type('application/json'); // set content-type
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
res.setHeader('Content-Type', 'application/json');
//Connect to db, return an error if can't connect
con.connect(function getData(err){
if(!err){
} else {
console.log('Error connecting to Db');
return;
}
});
//Query the db and output the results to the console
con.query('SELECT * FROM Data', function(err, rows)
{
if (err){
console.log('error');
} else {
res.send(JSON.stringify(rows));
}
});
//End the connection to the db
con.end(function(err) {
});
});
app.listen(process.env.PORT || 8888);
JSON example (substituted with dummy data and only two entries shown)
[{
"Name": "John Wise",
"Address": "111 Oak Street",
"Email": "dhfjdhfj@gmail.com",
"Phone": "4578399021",
"Fax": "49859",
"Job": "Human Resources",
"University": "New York Univeristy",
"City": "New York",
"State": "New York",
"Zip": "34123",
"Country": "United States",
"Status": ""
}, {
"Name": "Maria Petrova",
"Address": "4857 5th Street",
"Email": "fjdgikfj@gmail.com",
"Phone": "4958968956",
"Fax": "45465",
"Job": "Data Analyst",
"University": "Northwestern University",
"City": "New York",
"State": "New York",
"Zip": "34545",
"Country": "United States",
"Status": ""
}]
This discussion has been closed.
Answers
Solved it, had the columns within my AJAX call.
$(document).ready(function() {
$('#dataTables-example').DataTable( {
"serverSide": false,
"processing": true,
"ajax":{
"url":"http://localhost:8888/data",
"dataType": "JSON",
"dataSrc": "",
"aLengthMenu": "25",
},
"columns": [
{ "data": "Name" },
{ "data": "Address"},
{ "data": "Email" },
{ "data": "Phone" },
{ "data": "Fax"},
{ "data": "Job" },
{ "data": "University" },
{ "data": "City" },
{ "data": "State" },
{ "data": "Zip" },
{ "data": "Country" },
{ "data": "Status" }
]
```