Server-side using Perl, HTML in JSON...

Server-side using Perl, HTML in JSON...

KonerakKonerak Posts: 1Questions: 0Answers: 0
edited October 2009 in General
Hi all,

I've been using Datatables for some time and decided last week to switch to Server-side processing, using the new excellent 1.5 release. Since our back-end is completely Perl, this required some trickery to get the JSON correct. After that, it all worked. Maybe I'll post my final (Perl-)code later as example for future adventurers.

Some problems I've encountered and their solutions:
* When Datatables show no rows, 99% of the times, your JSON was invalid. Too bad Datatables or JQuery don't alert of this. A hint: have your script print the input-parameters, the generated SQL-command & the generated output to a file, so you can validate the JSON yourself should you get an empty table. The excellent http://www.jsonlint.com/ as found on this forum is quite helpful for this.

* If in your Datatables constructor you call "bServerSide: true" but your Datatables version is below 1.5, it will not complain. It'll just use the sAjaxSource and add no parameters. I forgot to update the Datatables script and found this out the hard way :D

* To get HTML in your JSON, don't forget to escape all characters properly. Especially the CR/LF can be tricky.

Once I got these errors (all my part - no offense meant) fixed, Datatables worked like a charm! We're absolutely amazed and are looking forward to converting other scripts to use Datatables as well.

Tags: HTML / JSON / No rows / Server-Side / Serverside / Perl / MySQL / jsonlint / CGI / DBI

Replies

  • allanallan Posts: 61,795Questions: 1Answers: 10,115 Site admin
    Hi Konerak,

    Thanks very much for your post. Great to hear that you've been working on a Perl script which can do the output required by DataTables. If you would be willing to post your source, then perhaps I can modify it to work with my example database (and as such it would be directly comparable to the PHP version). This could then be the basis for having an example backend in various different programming languages to help people get into the server-side processing option.

    Regarding your specific points:

    1. The JSON invalid issue - I honestly can't understand why jQuery doesn't throw an error when it encounters JSON that can't be parsed. I now wish that I has used $.ajax() rather than $.getJSON(), as it would then be easy to add an event handler for the error - but that would break compatibility with the 1.5.x branch. I will see if I can improve on this somehow...

    2. Yup - DataTables will ignore initialisation parameters it does not know. I've had thoughts about a debug version of DataTables which would flag this kind of thing up - but it would probably be part of a major overhaul of DataTables if it happens (all comes down to time sadly).

    3. As much as I love Javascript, I can't stand the fact that it's handling of multiline variables is appalling... Ah for a Perl here-doc like syntax :-)

    Regards,
    Allan
This discussion has been closed.