'length' is null or not an object
'length' is null or not an object
Just want to start by saying this is a great tool. Especially when you are working with a lot of data like we do in my work. Finding mutliple methods for display make it harder to develop a great solution, and this offers it all.
The only thing I seem to having is I get the message, 'length' is null or not an object, when I load the script. here is the intialization setup
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": '../data/clients.php'
} );
} );
Pretty simple, the data loads, but I get that one error, and I think it is causing the other issue I am having. Nothing "shows" up, untile I click on the column sort to desc, then again to asc then the data is there. Can I get some guidence on where this might be going aray.
Thanks in advance.
The only thing I seem to having is I get the message, 'length' is null or not an object, when I load the script. here is the intialization setup
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": '../data/clients.php'
} );
} );
Pretty simple, the data loads, but I get that one error, and I think it is causing the other issue I am having. Nothing "shows" up, untile I click on the column sort to desc, then again to asc then the data is there. Can I get some guidence on where this might be going aray.
Thanks in advance.
This discussion has been closed.
Replies
Showing 1 to 0 of 681 entries
It sounds rather like you might have the arrangement of the returned data in a format that DataTables doesn't recognise. Can you post an example of your json return?
The json (for client-side processing with an ajax source) should be like this: http://datatables.net/examples/media/examples_support/json_source.txt
The formatting for the server-side processing is very similar. You can see the return from the example on this page: http://datatables.net/1.5-beta/examples/data_sources/server_side.html
It's also worth validating your json:
http://www.jsonlint.com/
Hope this helps,
Allan
Should the text within the brackets be double quoted?
So with the original issue - are you using the expect json format? Can you provide an example of what you are using?
Thanks
Allan
I am using a PHP script to write the json, as the data is dynamic for the data table, it works when it comes through, minus what I said in the original post. ie, shows as processing, click the sort button goes to the end of the list, click it again all is fine. Dont want to have to do that.
The php file writes out the exact same as the json_source.txt file. With the exception that it only needs to output two columns instead of four. If I recall, I used the php script supplied in 1.4.3 examples.
<?php
function runSQL($rsql) {
$db['default']['hostname'] = "localhost";
$db['default']['username'] = 'xxxxxxxxx';
$db['default']['password'] = "xxxxxxxxx";
$db['default']['database'] = "xxxxxxxxx";
$db['live']['hostname'] = 'localhost';
$db['live']['username'] = "xxxxxxxxx";
$db['live']['password'] = "xxxxxxxxx";
$db['live']['database'] = "xxxxxxxxx";
$active_group = 'default';
$base_url = "http://".$_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
if (strpos($base_url,'webplicity.net')) $active_group = "live";
$connect = mysql_connect($db[$active_group]['hostname'],$db[$active_group]['username'],$db[$active_group]['password']) or die ("Error: could not connect to database");
$db = mysql_select_db($db[$active_group]['database']);
$result = mysql_query($rsql) or die ($rsql);
return $result;
mysql_close($connect);
}
function countRec($fname,$tname) {
$sql = "SELECT count($fname) FROM $tname ";
$result = runSQL($sql);
while ($row = mysql_fetch_array($result)) {
return $row[0];
}
}
$page = $_POST['page'];
$rp = $_POST['rp'];
$sortname = $_POST['sortname'];
$sortorder = $_POST['sortorder'];
if (!$sortname) $sortname = 'cust_No';
if (!$sortorder) $sortorder = 'desc';
$sort = "ORDER BY $sortname $sortorder";
if (!$page) $page = 1;
if (!$rp) $rp = 1000;
$start = (($page-1) * $rp);
$limit = "LIMIT $start, $rp";
$query = $_POST['query'];
$qtype = $_POST['qtype'];
$where = "";
if ($query) $where = " WHERE $qtype LIKE '%$query%' ";
$sql = "SELECT ID,cust_No,cust_Name FROM client_customer $where $sort";
$result = runSQL($sql);
$total = countRec("cust_No","client_customer $where");
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
$json = "";
$json .= "{ \"aaData\": [\n";
$rc = false;
while ($row = mysql_fetch_array($result)) {
if ($rc) $json .= "";
$json .= " [ \"".$row['cust_No']."\"";
$json .= ", \"".addslashes($row['cust_Name'])."\" ],\n";
$rc = true;
}
$json .= "]}";
echo $json;
?>
Where are the POST variables coming from? $_POST['query'] for example (which looks like a security hole - unescaped user input in a SQL command :-) ) - DataTables doesn't post any data when getting and Ajax source file (without server-side processing). Although it does look like you have 'if' conditions to catch no post.
Can you post a link please? It would make debugging much easier :-). If you don't want to make it public you and send it to me directly: http://www.datatables.net/contact .
Thanks
Allan
I have responded via contact form to you.
Thanks,
Richard
I have figured out where the error lies, the PHP script is throwing an extra , at the end:
[ "X0690192", "Sundog Cash Sale" ],
] }
So it is occuring with the PHP script that I wrote, I just need to figure out a way to strip the last comma from the end of the array, and all should be okay.
Thanks for your help on this, if you didn't mention the things you did I might still be lost and almost bald.
Richard
Good stuff - sorry I didn't back back to you before you found the issue - good to hear you got it sorted though. I find that jsonlint is exceptionally useful for debugging what is going on with json. It's a shame that jQuery doen'ts have built in support for parsing json other than eval() (although DataTables will use json2.js if it's available). Making the error fail over here more robust is something I'm meaning to do in DataTables...
Regards,
Allan
Totally agree with your points there. But I do admit DataTables is a great toolset, kudos!
After reviewing a bit more of my php script, I noticed I had a lot of "useless" code in there, cleaned it up quite a bit and boy does it work now! For those of you that may run into the same issues I had, my code is below. However you will notice my connection string is not included, it's the basic PHP mySQL connection string that you're all familiar with. But again I have to thank you allan for pointing out where to look. That JSON Validator is a great tool for helping you figure it out. I just wish they had the line numbers or it highlighted the error they point reference to, rather than just display it below. All in all, great resource.
The PHP Script to create the JSON source file:
<?php require_once('../Connections/Orion.php'); ?>
<?php
mysql_select_db($database_Orion, $Orion);
$query_clients = "SELECT ID, cust_No, cust_Name FROM client_customer ORDER BY cust_Name ASC";
$clients = mysql_query($query_clients, $Orion) or die(mysql_error());
$row_clients = mysql_fetch_assoc($clients);
$totalRows_clients = mysql_num_rows($clients);
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
$json = "";
$json .= "{ \"aaData\": [\n";
while ($row_clients = mysql_fetch_array($clients)) {
$json .= " [ \"".$row_clients['cust_No']."\"";
$json .= ", \"".$row_clients['cust_Name']."\" ],\n";
}
$json .= "] }";
$find = "],\n] }";
$replace = "] \n] }";
echo str_replace($find, $replace, $json);
mysql_free_result($clients);
?>
I have found here much useful information in
pharmacy
affiliate
I have found here much useful information in
http://pharmacyaffiliate.biz