Undefined aData.length on Ajax Call
Undefined aData.length on Ajax Call
First off this is a great addition to jQuery. Thanks a bunch! Been loving using the tables. Now I'm just having some trouble with the Ajax hook.
I've setup the following test for using DataTables with Ajax.
http://autojobs.aj.dneely.re.dev.getloaded.com/admin/dtTest.php
However, I'm getting the following error. What am I doing wrong? Please help! I modified the alert for "Warning - added data does not match known number of columns" so that I could see what was in my aData object when it comes back. Looks right to me.
What I'm seeing is that aData.length = undefined and therefore is not rendering the table.
Here is my code:
dtResp.php (Ajax calls this page)
[code]
<?php
$iTotalRecords = 4;
$iTotalDisplayRecords = 4;
$sEcho = $_GET['sEcho'];
$sColumns = "col1, col2, col3, col4";
$aaData = array (
array("d1"=>1, "d2"=>"data1", "d3"=>"more data 1", "d4"=>"testing 1"),
array("d1"=>2, "d2"=>"data2", "d3"=>"more data 2", "d4"=>"testing 2"),
array("d1"=>3, "d2"=>"data3", "d3"=>"more data 3", "d4"=>"testing 3"),
array("d1"=>4, "d2"=>"data4", "d3"=>"more data 4", "d4"=>"testing 4")
);
$jsonArray = array (
"iTotalRecords"=>$iTotalRecords,
"iTotalDisplayRecords"=>$iTotalDisplayRecords,
"sEcho"=>$sEcho,
"sColumns"=>$sColumns,
"aaData"=>$aaData
);
echo json_encode($jsonArray);
?>
[/code]
dtTest.php (display page)
[code]
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "dtResp.php"
} );
} );
Col 1
Col 2
Col 3
Col 4
Empty Set>
Col 1
Col 2
Col 3
Col 4
[/code]
I've setup the following test for using DataTables with Ajax.
http://autojobs.aj.dneely.re.dev.getloaded.com/admin/dtTest.php
However, I'm getting the following error. What am I doing wrong? Please help! I modified the alert for "Warning - added data does not match known number of columns" so that I could see what was in my aData object when it comes back. Looks right to me.
What I'm seeing is that aData.length = undefined and therefore is not rendering the table.
Here is my code:
dtResp.php (Ajax calls this page)
[code]
<?php
$iTotalRecords = 4;
$iTotalDisplayRecords = 4;
$sEcho = $_GET['sEcho'];
$sColumns = "col1, col2, col3, col4";
$aaData = array (
array("d1"=>1, "d2"=>"data1", "d3"=>"more data 1", "d4"=>"testing 1"),
array("d1"=>2, "d2"=>"data2", "d3"=>"more data 2", "d4"=>"testing 2"),
array("d1"=>3, "d2"=>"data3", "d3"=>"more data 3", "d4"=>"testing 3"),
array("d1"=>4, "d2"=>"data4", "d3"=>"more data 4", "d4"=>"testing 4")
);
$jsonArray = array (
"iTotalRecords"=>$iTotalRecords,
"iTotalDisplayRecords"=>$iTotalDisplayRecords,
"sEcho"=>$sEcho,
"sColumns"=>$sColumns,
"aaData"=>$aaData
);
echo json_encode($jsonArray);
?>
[/code]
dtTest.php (display page)
[code]
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "dtResp.php"
} );
} );
Col 1
Col 2
Col 3
Col 4
Empty Set>
Col 1
Col 2
Col 3
Col 4
[/code]
This discussion has been closed.
Replies
[code]
$aaData = array (array("1", "d2", "d1", "d4"),
array("2", "d2", "d1", "d4"),
array("3", "d2", "d1", "d4"),
array("4", "d2", "d1", "d4"));
$jsonArray = array (
"iTotalRecords"=>$iTotalRecords,
"iTotalDisplayRecords"=>$iTotalDisplayRecords,
"sEcho"=>$sEcho,
"aaData"=>$aaData
);
echo json_encode($jsonArray);
[/code]
Thanks for the kind words about DataTables :-). Good to hear you got this sorted - thanks for the update.
Regards,
Allan
I am new to datatables plugin.
I am facing same issues related to server side processing.
I am making json string in correct format but I am getting error aData is undefined.
Json: [{"aaData": [["Gecko", "FirefoxA", "hello", "world", "hi"], ["Gecko", "FirefoxA", "hello", "world", "hi"]], "iTotalRecords": 3, "sEcho": 1, "iTotalDisplayRecords": 3}]
html:
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "gethttpjsonresponse",
});
} );
Col 1
Col 2
Col 3
Col 4
Col 5
I think I am making some silly mistake up here.
I was able to get data via ajax datasource and javascript array with same html.
Any help will be greatly appreciated.
Thanks,
shardul
The server-side examples show the expected format: http://datatables.net/release-datatables/examples/data_sources/server_side.html
Allan
Skorupa
here is my json:
{"sEcho":4,"iTotalRecords":"10000","iTotalDisplayRecords":"10000","aaData":[["6135","Jerome","Moreno","Costa Rica","X6R 7H7"],["6136","Pearl","Simpson","Japan","43428"],["6137","Virginia","Warner","Spain","Z2C 3M1"],["6138","Michael","Davidson","El Salvado","38595"],["6139","Rosalyn","Mayer","Poland","84438"],["6140","Cullen","Munoz","Sri Lanka","41933"],["6141","Nomlanga","Andrews","Lesotho","Q7B 3C9"],["6142","Althea","Boone","Vanuatu","G7R 1F0"],["6143","Magee","Salas","Lesotho","U7L 9S8"],["6144","Aladdin","Day","Zambia","A1X 1K9"],["6145","Athena","Boyle","Sierra Leo","32203"],["6146","Myles","Humphrey","Bolivia","08372"],["6147","Evelyn","Gutierrez","Bhutan","25727"],["6148","Maxine","Camacho","Portugal","33586"],["6149","Cecilia","Mejia","Antarctica","P8Y 2W7"],["6150","Declan","Monroe","Japan","01437"],["6151","Octavia","English","Mexico","05238"],["6152","Justina","Collins","United Sta","42700"],["6153","Violet","Wiggins","Guatemala","S6M 5Q6"],["6154","Yuri","Davis","Turkey","P8U 6J0"],["6155","Gabriel","Alexander","Tajikistan","U5R 1I6"],["6156","Echo","Bray","El Salvado","M6R 9D2"],["6157","Clinton","Bates","Kazakhstan","18794"],["6158","Mira","Beck","Guadeloupe","Y7D 5J1"],["6159","Justin","Morton","Iran, Isla","94251"],["6160","Brielle","Torres","Zimbabwe","76795"],["6161","Asher","Hensley","Maldives","94991"],["6162","Otto","Banks","Cayman Isl","F6P 1N0"],["6163","Eagan","Graham","Sierra Leo","65342"],["6164","Emmanuel","Burnett","Iceland","27222"],["6165","Martina","Bean","Angola","N9K 7N9"],["6166","Honorato","Baird","Cocos (Kee","C2R 7U0"],["6167","Aristotle","Sosa","Congo","88860"],["6168","Lara","Reynolds","Croatia","G4R 2U4"],["6169","India","Prince","Bahrain","73890"],["6170","Joel","Crosby","Cameroon","72450"],["6171","Cleo","Larson","Lithuania","B4K 4T0"],["6172","Barrett","Blanchard","Netherland","H7C 1B9"],["6173","Emery","Kirkland","Brazil","14548"],["6174","Libby","Sosa","Gabon","R4L 1U7"],["6175","Fulton","Murray","Oman","J6O 1R6"],["6176","Indigo","Cantrell","French Gui","H5K 7Z7"],["6177","Tamekah","Owen","Romania","49199"],["6178","Lawrence","Foster","Comoros","00165"],["6179","Abraham","Frank","Sweden","Y8R 3I0"],["6180","Florence","Stark","Belize","41491"],["6181","Cally","Collins","Hungary","58358"],["6182","Raven","Osborne","Saudi Arab","46244"],["6183","Summer","Rowe","Kyrgyzstan","60735"],["6184","Macaulay","Justice","Seychelles","K4D 9X9"],["6185","Maggie","Mercado","El Salvado","61517"],["6186","Raymond","Gross","Northern M","30501"],["6187","Brynn","Hartman","Ireland","E2I 6M4"],["6188","Felix","Phelps","Congo","82554"],["6189","Eric","Nguyen","Nicaragua","34565"],["6190","Lunea","Vang","Micronesia","A3U 2L1"],["6191","Lesley","Mills","Seychelles","I1B 2F6"],["6192","Mira","Hoover","Burkina Fa","V7V 3H4"],["6193","Nell","Campbell","Benin","Y2T 7J1"],["6194","Craig","Grant","Dominica","K4K 5P6"],["6195","Serena","Callahan","Christmas ","80663"],["6196","Nevada","Lyons","South Geor","H6H 5J9"],["6197","Octavia","Murray","Czech Repu","62306"],["6198","Alika","Knight","Ukraine","04885"],["6199","Simon","Butler","Western Sa","Q2S 9T9"],["6200","Steel","Morin","Guinea-bis","R8E 3A5"],["6201","Sandra","Mcintyre","India","W3B 4D9"],["6202","Judith","Morin","Greenland","90969"],["6203","Nell","Jackson","Dominica","65547"],["6204","Philip","Blair","Chile","C6G 9Y2"],["6205","Malachi","Melton","Philippine","R6B 6Q0"],["6206","Zia","Cunningham","Swaziland","95550"],["6207","Zia","Maldonado","Tokelau","L1O 1H5"],["6208","Whilemina","Coleman","Northern M","T2T 3E3"],["6209","Brennan","Hooper","Burkina Fa","85541"],["6210","Geraldine","Pruitt","Sierra Leo","H6S 3E7"],["6211","Tamekah","Wooten","Comoros","Q8X 7P2"],["6212","Tate","Harrington","Tajikistan","Q8A 3I4"],["6213","Oren","Coleman","Bermuda","61134"],["6214","Angela","Henderson","Hungary","Z3S 8E4"],["6215","Kylan","Winters","Romania","P5C 4O3"]]}