Server Side Error

Server Side Error

HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
edited May 2012 in DataTables 1.9
Hi all!

Im a newbie on DataTables, so be patient please.

Im trying to use server side example for my web, but when i change data on code, only mysql info, ive this ERROR:

DataTAbles warning(table id = 'example'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.

Anybody knows how to fix it?

Thanks! And sorry about my english, im spanish and i'm trying to write as clear as posible :D
«1

Replies

  • PeteBPeteB Posts: 38Questions: 0Answers: 0
    Are you using the sample script?

    Here's the top part of one of mine: [code] /*
    * Script: DataTables server-side script for PHP and MySQL
    * Copyright: 2010 - Allan Jardine
    * License: GPL v2 or BSD (3-point)
    */
    /* Array of database columns which should be read and sent back to DataTables. Use a space where
    * you want to insert a non-database field (for example a counter or static image)
    */
    $plyrno = $_GET['playerno'];
    $aColumns = array( 'ahID', 'ahseasonID', "ahT", 'ah1stXV', 'ah2ndXV', 'ah3rdXV', 'ah4thXV', 'ah5thXV', 'ahOthXV', 'jnrapps', 'ahplayerID');

    /* Indexed column (used for fast and accurate table cardinality) */
    $sIndexColumn = "ahID";

    /* DB table to use */
    $sTable = "apphistory";
    $sTable = $sTable . ' WHERE (' . $sTable . '.ahplayerID = ' . $plyrno . ')';

    include('include/conn.php'); [/code]

    One of the most important parts is that the array or columns must match the number of columns in your html table.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Also, run your table through the debugger and let us know the code.

    Allan
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    edited May 2012
    Here is the code, thx for the help!

    http://live.datatables.net/usopol/edit#javascript,html,live

    PeteB, my code is different, so i can't use yours, but thank you anyway.

    Allan, i'm using the example code from the web and i only change the connection info. If you need this info, i can give you by a message. Thank you!
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Running the table through the debugger would be very helpful as it should let us see what your server is responding with.
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    Sorry, i didn't understand. Here is the debugger: http://debug.datatables.net/epeqah
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    If you click on the "Tables" tab in the debugger it says 0 tables - which suggests that there was a Javascript error on your page while initialising the tables, or that the initialisation for the table hadn't yet run. What does Firebug say?

    Allan
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    edited May 2012
    When i load the page, it says that i miss a semi-colon when i declare "sAjaxSorce" , but i didnt. I give you the codes of server_processing.php and server_side.html

    [code]<?php
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * Easy set variables
    */

    /* Array of database columns which should be read and sent back to DataTables. Use a space where
    * you want to insert a non-database field (for example a counter or static image)
    */
    $aColumns = array( 'FechaActa', 'AñoActa', 'Signatura', 'Organo', 'Pagini' );

    /* Indexed column (used for fast and accurate table cardinality) */
    $sIndexColumn = "Num";

    /* DB table to use */
    $sTable = "Actas";

    /* Database connection information */
    $gaSql['user'] = "*";
    $gaSql['password'] = "*";
    $gaSql['db'] = "*";
    $gaSql['server'] = "*";





    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * If you just want to use the basic configuration for DataTables with PHP server-side, there is
    * no need to edit below this line
    */

    /*
    * MySQL connection
    */
    $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
    die( 'Could not open connection to server' );

    mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
    die( 'Could not select database '. $gaSql['db'] );


    /*
    * Paging
    */
    $sLimit = "";
    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
    {
    $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 $iFilteredTotal,
    "aaData" => array()
    );

    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
    $row = array();
    for ( $i=0 ; $i[/code]

    [code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">





    DataTables example

    @import "../../media/css/demo_page.css";
    @import "../../media/css/demo_table.css";




    $(document).ready(function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "/scripts/server_processing.php";
    } );
    } );





    DataTables server-side processing example







    Customer_Ticket_No




    Loading data from server




    prueba1
    prueba1
    prueba1
    prueba1
    prueba1





    [/code]
  • hozthozt Posts: 45Questions: 0Answers: 0
    What you have is an extra ;
    Here:
    [code]
    "sAjaxSource": "/scripts/server_processing.php";[/code]

    This is JSON, use a validator to check your JSON, and listen to your browser's complains.

    http://jsonlint.com/
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    hozt

    I haven't got a JSon Code, im getting data from a MySQL tables. By the moment, the error of JSon dissapear, but data from MySQL didnt load. Any idea?
  • hozthozt Posts: 45Questions: 0Answers: 0
    The DataTables initialization code is in JSON format, that's what I was saying, you should remove the ";" and the end of the sAjaxSource line.

    About the not loading data, check in the console if the post went through, and what is the response.
    If the response is empty, then it's just a problem on your php script.
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    So, i have to remove all the sAjaxSource declaration line?
  • hozthozt Posts: 45Questions: 0Answers: 0
    No, this
    [code] "sAjaxSource": "/scripts/server_processing.php";[/code]
    to this:
    [code] "sAjaxSource": "/scripts/server_processing.php"[/code]

    The last line doesn't need ; or ,
    It's what let the plugin knows not to expect any more parameters.
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    edited May 2012
    I did it and now i have this code:
    [code]
    $(document).ready(function() {
    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "/scripts/server_processing.php"
    } );
    } );
    [/code]

    And Browser's error says that it waits } on line of "sAjaxSource", but i have 2 } for close function.
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    Solved, but now load of data didn't work. It stay on Processing... message and didnt load. I only change the conec info, and i read in other cases that it work, but i cant load data from server.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Good - so now use the debugger and let us know what the debug code is :-)
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    Here is the new debugger code :D Thank you all for this patient, i really apreciatte it :D

    http://debug.datatables.net/oqebij
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    It would appear that your server-side script isn't returning anything. You can confirm this by using Firebug or Inspector in Webkit browsers.

    Allan
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    0 rows, but i dont know why my server isn't returning anything. I take the same number of columns (5) and i didn't change nothing from where/order/limit instructions. I don't know how to do it works. Any idea?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Nope - what does your server error log say?
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    My server error log? I haven't got it. Maybe i need to change where clausule or the order by? My table of MySQL has 30 columns, but i only need 5, maybe this is the bug?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    > My server error log? I haven't got it

    Typically in /var/log/apache/error_log on your server. Either that or make sure PHP is set to output errors.
  • hozthozt Posts: 45Questions: 0Answers: 0
    edited May 2012
    I edited my entire comment, sleepy mode..
    Yes you are using Allan's Server-side code which was my first question and there's a mysql_error() implemented on it, my guess is then your select is not faulty, but it's just malformed or the parameters you are passing return nothing from the database.

    But it would still return something.. Hum.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I could indeed return something, which is what is making me think that something is setup to swallow the errors form the output. Checking the error log should confirm if this is the case.
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    How can i see that error log in my server? I'm working on a free host server, 000webhost. I could work in localhost with apache?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Contact your hosting provider to find that out. Or set up a sever where you can access the log.

    Allan
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    edited May 2012
    Well, i have created a mysql server on my pc, now i'll probe with it. I'll give you the results, thans for all.
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    With this code no errors and 5 rows returned, but my page didn't load it.

    [code]
    <?php
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * Easy set variables
    */

    /* Array of database columns which should be read and sent back to DataTables. Use a space where
    * you want to insert a non-database field (for example a counter or static image)
    */
    $aColumns = array( 'dato1', 'dato2', 'dato3', 'dato4', 'dato5' );

    /* Indexed column (used for fast and accurate table cardinality) */
    $sIndexColumn = "dato1";

    /* DB table to use */
    $sTable = "prueba";

    /* Database connection information */
    $gaSql['user'] = "root";
    $gaSql['password'] = "";
    $gaSql['db'] = "tablas_urnieta";
    $gaSql['server'] = "localhost:3306";





    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * If you just want to use the basic configuration for DataTables with PHP server-side, there is
    * no need to edit below this line
    */

    /*
    * MySQL connection
    */
    $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
    die( 'Could not open connection to server' );

    mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
    die( 'Could not select database '. $gaSql['db'] );


    /*
    * Paging
    */
    $sLimit = "";
    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
    {
    $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 $iFilteredTotal,
    "aaData" => array()
    );

    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
    $row = array();
    for ( $i=0 ; $i
    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited May 2012
    > echo $output ;

    That looks a bit dodgy - you need to json_encode() it.

    Also can you run your table through the debugger if the script it returning data (i.e. you can see it in Firebug) but still now working.
  • HodeitxoHodeitxo Posts: 16Questions: 0Answers: 0
    i've put the json_encode() and it still without loading. I install the firebug, but i don't know how to use it. What i need to find here?

    Thanks all :D
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited May 2012
    First hit on Google: www.youtube.com/watch?v=W4jXAaEMp2M :-)

    As I said - can you run it throughout he debugger again please if it isn't working.
This discussion has been closed.