For practice, I am trying to display JSOn data into a datatable in my aspx webpage, but the datatable is stuck with the word processing. Turns out when I inspect the console, the error is Cannot read property 'length' of undefined
What does this error mean? how do I fix it? is this error because of my wrong approach?
What I did is, first I made a .aspx.cs file that produces the JSON string whenever it's URL is called:
[{"NickName":"Bob","LogIN":"6/7/1846 12:00:00 AM","LogOUT":"6/7/1846 12:00:00 AM"},{"NickName":"Bob","LogIN":"6/6/1846 12:00:00 AM","LogOUT":"6/6/1846 12:00:00 AM"},{"NickName":"Patrick","LogIN":"6/7/1846 12:00:00 AM","LogOUT":"6/7/1846 12:00:00 AM"},{"NickName":"Ward","LogIN":"6/7/1846 12:00:00 AM","LogOUT":"6/7/1846 12:00:00 AM"},{"NickName":"Ward","LogIN":"6/6/1846 12:00:00 AM","LogOUT":"6/6/1846 12:00:00 AM"},{"NickName":"Ward","LogIN":"6/5/1846 12:00:00 AM","LogOUT":"6/5/1846 12:00:00 AM"},{"NickName":"Krabs","LogIN":"","LogOUT":""},{"NickName":"Sandy","LogIN":"","LogOUT":""},{"NickName":"Pearl","LogIN":"","LogOUT":""}]
Then on my .aspx file where I am supposed to display the datatable, I wrote this script which is the suggested on the dataTables site:
/*using the ajax function*/
var tbl = $('#datatableJSONEx').DataTable({
processing: true,
serverSide: true,
info: true,
ajax: "DefaultJSONResult?THEstr=produceJSON",
order: [[0, 'desc']],
select: true,
responsive: true,
buttons: true,
length: 10
});
Then I added it's representation in HTML:
<div>
<table id="datatableJSONEx" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>NickName</th>
<th>LogIN</th>
<th>LogOUT</th>
</tr>
</thead>
<tfoot>
<tr>
<th>NickName</th>
<th>LogIN</th>
<th>LogOUT</th>
</tr>
</tfoot>
</table>
</div>
</div>
What is causing the error? When the error has been addressed, is this approach correct if I want my tables to automatically update when there are changes to the server?
EDIT
I changed this property in my dataTables jQuery :
ajax: "DefaultJSONResult?THEstr=produceJSON"
into
ajax: $.ajax("DefaultJSONResult?THEstr=produceJSON")
however, it still displays processing.. and I can't see anything on the console anymore when I inspect element in google chrome.
UPDATE
I changed my jQuery to have $.ajax()
convert my url into an object, then I pass it into the ajax:
property of the datatable:
var jqxhr = $.ajax("DefaultJSONResult?THEstr=ProduceJSON",
{
success: function (data, status, xhr) { //on success
$('p').append(status);
},
error: function (jqXhr, textStatus, errorMessage) { //the action was a failure, know why.
$('p').append('ERROR: ' + errorMessage);
}
});
/*using the ajax function*/
var tbl = $('#datatableJSONEx').DataTable({
processing: true,
serverSide: true,
info: true,
ajax: jqxhr,
sAjaxDataProp : "",
columns: [
{ data: 'NickName'},
{ data: 'TimeIN' },
{ data: 'TimeOUT' }
],
order: [[0, 'desc']],
select: true,
responsive: true,
buttons: true,
length: 10
});
the element p
now returns SUCCESS
and there are no errors on the console showing. BUT STILL the datatable returns nothing... is the JSON string the .aspx url returning the cause? how can I make Jquery Datatables acccept my format? What is the correct format?