Faild to parse json by datatables

Faild to parse json by datatables

LordCoverLordCover Posts: 7Questions: 0Answers: 0
edited October 2010 in General
[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.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    You don't have an sEcho parameter - it would be worth adding that. It should be optional, but possibly not these days... Other than that, do you get any JS errors or anything?

    Allan
  • LordCoverLordCover Posts: 7Questions: 0Answers: 0
    I tried w/o sEcho parameter but no good results.

    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
  • LordCoverLordCover Posts: 7Questions: 0Answers: 0
    edited October 2010
    The rest is here:
    [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.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Interesting. Could you try the nightly version of DataTables please: http://datatables.net/download/ ? I made a small change for jQuery 1.4.3 - but it seems unlikely that it will make any difference.

    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
  • LordCoverLordCover Posts: 7Questions: 0Answers: 0
    I will download that and feedback the result.

    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.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Yes certainly. Open Firebug and click on the "console" tab. You'll see all XHRs which are made in there (you might need to enable the console, depending on the version of Firebug I think). You can click the little plus button to view the XHR data - and that will most likely tell you what is going on.

    Allan
  • LordCoverLordCover Posts: 7Questions: 0Answers: 0
    OK, thanks

    Problem solved, there was an error in an SQL query.

    Good Tool, Good Support.
  • LordCoverLordCover Posts: 7Questions: 0Answers: 0
    edited October 2010
    Oops!, I made some changes to the architecture of the website (folders).

    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.
  • LordCoverLordCover Posts: 7Questions: 0Answers: 0
    edited October 2010
    I've been checking this eror a lot time

    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
This discussion has been closed.