Editor-enabled DataTable displays the error message 'JSON data from server can't be parsed'

Editor-enabled DataTable displays the error message 'JSON data from server can't be parsed'

caneco2013caneco2013 Posts: 6Questions: 0Answers: 0
edited June 2013 in Editor
Hello to all. It seems that I'm still having some issues with regards to my Editor-enabled DataTable. At first I'm not getting any displayed data, I only see the formatted table headers. But then I realized what was causing it and that was I forgot to include [code]"bProcessing": true[/code], [code]"bServerSide": true[/code] and [code]"sServermethod": "POST"[/code].

So i included them in my DataTable initialization. I was hoping that it has solved the problem but surprisingly it triggered a new error. The new error that I am getting is the error message: [code]JSON data from server could not be parsed. This is caused by a JSON formatting error.[/code] Now I am clueless as to what's causing it. I already followed and double-checked every examples that I have read but still I get the same error. I even included the file config.php in my generated Editor code since I read it in the documentation of Editor. But I have a feeling that what's causing it is the directory strings I've provided for [code]sAjaxSource[/code] and [code]sSwfPath[/code] although I already tried to play around the directory string hoping that the data will finally be shown. Can someone please help me solve this?

This is my DataTables debugger link: http://debug.datatables.net/aziyeg

admin_masterlist.php - The page that shows the datatable:

[code]
<!doctype html>



<!--Stylesheets-->





<!--Scripts-->








/*$(document).ready(function(){
$("#admintable").dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "../../cnc_proc/proc_adminmaster.php",
"sServerMethod": "POST",
});
});*/


Administrator List










Record ID
Administrator ID
Password
Last Name
First Name
Administrator Type Code



































[/code]

Then this is the table.caneco_admintable.js - My generated Editor js code:

[code]

/*
* Editor client script for DB table caneco_admintable
* Automatically generated by http://editor.datatables.net/generator
*/

(function($){

$(document).ready(function() {
var editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "../../DataTables-1.9.4/extras/Editor-1.2.3/examples/php/table.caneco_admintable.php",
"domTable": "#admintable",
"fields": [
{
"label": "Administrator ID",
"name": "Admin_UserID",
"type": "text"
},
{
"label": "Administrator Password",
"name": "Admin_Passwrd",
"type": "password"
},
{
"label": "First Name",
"name": "Admin_FirstName",
"type": "text"
},
{
"label": "Last Name",
"name": "Admin_LastName",
"type": "text"
},
{
"label": "Administrator Type",
"name": "Admin_TypeCode",
"type": "select",
"ipOpts": [
{
"label": "1",
"value": "1"
},
{
"label": "2",
"value": "2"
},
{
"label": "3",
"value": "3"
},
{
"label": "4",
"value": "4"
}
]
}
]
} );

$('#admintable').dataTable( {
"sDom": "Tfrtip",
"sAjaxSource": "../../DataTables-1.9.4/extras/Editor-1.2.3/examples/php/table.caneco_admintable.php",
"bProcessing": true,
"bServerSide": true,
"sServerMethod": "POST",
"aoColumns": [
{
"mData": "Admin_UserID"
},
{
"mData": "Admin_Passwrd"
},
{
"mData": "Admin_FirstName"
},
{
"mData": "Admin_LastName"
},
{
"mData": "Admin_TypeCode"
}
],
"oTableTools": {
"sRowSelect": "multi",
"sSwfPath": "../../DataTables-1.9.4/extras/TableTools/media/swf/copy_csv_xls_pdf.swf",
"aButtons": [
{ "sExtends": "editor_edit", "editor": editor },
{ "sExtends": "editor_remove", "editor": editor }
]
}
} );
} );

}(jQuery));

[/code]

And finally table.caneco_admintable.php - the file being called within table.caneco_admintable.js

[code]
<?php

/*
* Editor server script for DB table caneco_admintable
* Automatically generated by http://editor.datatables.net/generator
*/

// DataTables PHP library
include( "lib/DataTables.php" );
include( "lib/config.php" );

// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;


// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'caneco_admintable' )
->fields(
Field::inst( 'Admin_UserID' )
->validator( 'Validate::required' ),
Field::inst( 'Admin_Passwrd' )
->validator( 'Validate::minMaxLen_required', array('min'=>8, 'max'=>25 ) ),
Field::inst( 'Admin_FirstName' )
->validator( 'Validate::required' ),
Field::inst( 'Admin_LastName' )
->validator( 'Validate::required' ),
Field::inst( 'Admin_TypeCode' )
->validator( 'Validate::required' )
)
->process( $_POST )
->json();


[/code]

I hope that someone will be able to help me. Thank you very much.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    If you have a look at the JSON response from the server in your browser's developer tools (or click the 'Tables and then 'Server interaction' tabs in the debug view) you'll see that right enough, the response is invalid JSON.

    > Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'field list'

    That's the problem.

    I guess you need to use the third parameter of the Editor institutionalisation option to tell it what the primary key should be ( http://editor.datatables.net/docs/current/php/class-DataTables.Editor.html#___construct ).

    Regards,
    Allan
  • caneco2013caneco2013 Posts: 6Questions: 0Answers: 0
    Thanks Allan! That really solved my problem :) Can you explain to me why it occured? Also, how can I avoid the error without using the solution you gave to me?

    By the way, this work of yours is really great. I hope that the development and support will continue through the years.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    The Editor libraries require a primary key on the table it is working with to uniquely identify each row, and the default is to assume that it is called `id` (as that's probably the most common case). If you have your primary key column called something else - you need to tell the libraries that is the case.

    > By the way, this work of yours is really great. I hope that the development and support will continue through the years.

    Thank you! I most certainly plan for DataTables, Editor and the rest of the project to have a long term future! 5 years and counting now - here is to another 5 (10, 15... :-) ).

    Regards,
    Allan
  • caneco2013caneco2013 Posts: 6Questions: 0Answers: 0
    Ah i see. Thanks again for your help :)
This discussion has been closed.