Faild to parse json by datatables
Faild to parse json by datatables
[code]
{
"iTotalRecords": 4,
"iTotalDisplayRecords": 4,
"aaData": [
[
"1",
"??? 1",
"????",
"??? ??????? ????????",
"??? ?????",
"",
"",
""
],
[
"2",
"??? 10",
"???? ???",
"?????????????",
"??? ??????",
"",
"",
""
],
[
"3",
"sfs",
"sfsefs",
"sfsf",
"??? ??????",
"",
"",
""
],
[
"4",
"sfe",
"qwewe",
"hjjhj",
"??? ?????",
"",
"",
""
]
]
}
[/code]
I've check this with jsonlint.com tool and it's valid but datatables cannot accept it and consequently cannot load the data into the table.
I'm using PHP5 and datatables-1.7.3
Edit: About ?, it is normal Arabic language characters and it is definitely not the problem.
{
"iTotalRecords": 4,
"iTotalDisplayRecords": 4,
"aaData": [
[
"1",
"??? 1",
"????",
"??? ??????? ????????",
"??? ?????",
"",
"",
""
],
[
"2",
"??? 10",
"???? ???",
"?????????????",
"??? ??????",
"",
"",
""
],
[
"3",
"sfs",
"sfsefs",
"sfsf",
"??? ??????",
"",
"",
""
],
[
"4",
"sfe",
"qwewe",
"hjjhj",
"??? ?????",
"",
"",
""
]
]
}
[/code]
I've check this with jsonlint.com tool and it's valid but datatables cannot accept it and consequently cannot load the data into the table.
I'm using PHP5 and datatables-1.7.3
Edit: About ?, it is normal Arabic language characters and it is definitely not the problem.
This discussion has been closed.
Replies
Allan
My story is that I had this all working fine on an old local Apache server (xampp) and now
I updated to wamp (wampserver.org) which updated PHP from v4 to v5.
and today I updated datatables from 1.5.6 to 1.7.3.
Here is the warning message I get on both (IE6, FireFox, Chrome):
DataTables warning: JSON data from server failed to load or be parsed. This is most likely to be caused by a JSON formatting error.
Here is the code for the data loader (sAjaxSource file):
[code]
<?php
/* MySQL connection */
session_start();
include_once("../common/top.php");
if(isset($_SESSION['admin']) && $_SESSION['admin']=='admin')
{
function fnColumnToField( $i )
{
if ( $i == 0 )
return "id";
else if ( $i == 1 )
return "title";
else if ( $i == 2 )
return "joker";
else if ( $i == 3 )
return "joke";
else if ( $i == 4 )
return "category";
else if ( $i == 5 )
return "add_date";
else if ( $i == 6 )
return "last_update";
}
/* Paging */
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) )
{
$sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).",".
mysql_real_escape_string( $_GET['iDisplayLength'] );
}
/* Ordering */
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i
[code]
$sQuery = "SELECT id, title, joker, joke, category
FROM jokes ".$sWhere.$sOrder.$sLimit;
//echo $sQuery;
$rResult = mysql_query($sQuery) or die(mysql_error());
$sQuery = "
SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query($sQuery) or die(mysql_error());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];
$sQuery = "
SELECT COUNT(id)
FROM jokes
";
$rResultTotal = mysql_query($sQuery) or die(mysql_error());
$aResultTotal = mysql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];
$sOutput = '{';
$sOutput .= '"sEcho": '.intval($_GET['sEcho']).', ';
$sOutput .= '"iTotalRecords": '.$iTotal.', ';
$sOutput .= '"iTotalDisplayRecords": '.$iFilteredTotal.', ';
$sOutput .= '"aaData": [ ';
while ( $aRow = mysql_fetch_array( $rResult ) )
{
$category=mysql_query("SELECT name FROM category WHERE id=".$aRow['category']);
if(mysql_num_rows($category)>0){
$category_title=mysql_fetch_array($category);
}else{
$category_title=array();
}
$sOutput .= "[";
$sOutput .= '"'.addslashes($aRow['id']).'",';
$sOutput .= '"'.addslashes($aRow['title']).'",';
$sOutput .= '"'.addslashes($aRow['joker']).'",';
$sOutput .= '"'.addslashes(str_replace("\r\n","
",$aRow['joke'])).'",';
$sOutput .= '"'.addslashes($category_title['name']).'",';
$sOutput .= '"'.addslashes($aRow['add_date']).'",';
$sOutput .= '"'.addslashes($aRow['last_update']).'",';
$sOutput .= "\"\",";
$sOutput .= "\"\",";
$sOutput .= "\"\"";
$sOutput .= "],";
}
$sOutput = substr_replace( $sOutput, "", -1 );
$sOutput .= '] }';
echo $sOutput;
// Write json to a file (for verfication purposes)
$fileHandle = fopen('data.txt', 'w+')
OR die ("Can't open file\n");
$result = fwrite ($fileHandle, $sOutput);
fclose($fileHandle);
}else{
echo '
<!--
window.location = "'._PREF.'admin/index.php"
//-->
';
}
?>
[/code]
the new JSON:
[code]
{
"sEcho": 1,
"iTotalRecords": 4,
"iTotalDisplayRecords": 4,
"aaData": [....
[/code]
The rest of the file is exactly the same as above, and yet again it was tested to be valid JSON.
More code can be submitted upon request.
If you are getting a JSON error, then that is being thrown by jQuery, and it's implementation of a JSON parsers is strict - so it must be broken somewhere. If you could post the full JSON returned from the server when you get that error, that would be very useful.
Allan
But about JSON return from the server, today, I installed FireBug, but it has a lot of elements which I lost in and didn't know where to find that JSON, that I hear people here talking about, so could you please guide me where to find that?
Thanks anyway.
Allan
Problem solved, there was an error in an SQL query.
Good Tool, Good Support.
and now, the data cannot be loaded, the invalid JSON message is popping up again.
and in the FireBug, the Console tab is empty, though, I'm using Net tab, it has a sub-tab named XHR
it shows:
[code]
Failed to load source for: http://localhost/advjokes0/modules/jokes/admin/loadJoke.php?_=1288177811419&sEcho=1&iColumns=10&sColumns=&iDisplayStart=0&iDisplayLength=10&sSearch=&bRegex=false&sSearch_0=&bRegex_0=false&bSearchable_0=true&sSearch_1=&bRegex_1=false&bSearchable_1=true&sSearch_2=&bRegex_2=false&bSearchable_2=true&sSearch_3=&bRegex_3=false&bSearchable_3=true&sSearch_4=&bRegex_4=false&bSearchable_4=true&sSearch_5=&bRegex_5=false&bSearchable_5=true&sSearch_6=&bRegex_6=false&bSearchable_6=true&sSearch_7=&bRegex_7=false&bSearchable_7=false&sSearch_8=&bRegex_8=false&bSearchable_8=false&sSearch_9=&bRegex_9=false&bSearchable_9=false&iSortingCols=1&iSortCol_0=0&sSortDir_0=asc&bSortable_0=true&bSortable_1=true&bSortable_2=true&bSortable_3=true&bSortable_4=false&bSortable_5=true&bSortable_6=true&bSortable_7=false&bSortable_8=false&bSortable_9=false[/code]
What does this message mean?
P.S: I've checked the new situation for the previous error and it is not the case here, and I tested JSON and it is valid.
and here are my results:
1. the page loadJoke.php is not being called at all from the page listJokes.php.
2. the parameters after loadJoke.php are always the same.
3. JSON script generation is not being reached since the page loadJoke.php is not being called in the first place.
4. Do you think that dealing with relative/absolute paths in PHP could produce datatables/jquery bugs?
Another thing: What about this "_" parameter in the previous loadJoke.php GET parameters list?
thanks