Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^4: Server-side processing?

by frank1 (Scribe)
on Feb 14, 2024 at 12:41 UTC ( [id://11157691]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Server-side processing?
in thread Server-side processing?

i dont know, i was just reading some article, to be able to handle multi records in datatable. and i found out that i need to do server side processing . and i came up with my script to find any help from anyone

and most articles talking about returning data from database query to json data and return it to html. which is i dont know

Replies are listed 'Best First'.
Re^5: Server-side processing?
by hippo (Bishop) on Feb 14, 2024 at 13:52 UTC
Re^5: Server-side processing?
by marto (Cardinal) on Feb 14, 2024 at 13:09 UTC

    In the page you've shown, the JavaScript DataTable calls a perl script, that needs to return a JSON object rather than HTML. On success DataTables will render a grid populated with the results. For my use case (Mojolicious::Lite) this is as simple as the route returning the results via something like this:

    $c->render( json => $griddata );

    For your example you don't show a SSCCE, so unless you're using some framework you'll need to use a JSON module and return the results appropriately. DataTables perl modules I haven't used DataTables, Mojolicious::Plugin::DataTables. Looking at multiple previous threads, I think you'd get more benefit from taking the time to make sure your questions are more easily understood to avoid repetition. How do I post a question effectively?.

Re^5: Server-side processing?
by 1nickt (Canon) on Feb 14, 2024 at 12:49 UTC

    You need a module that will convert your Perl data structure into JSON. See for example JSON. You do not "return it to HTML". Once you have the JSON data, you return that in response to the Ajax call.

    Hope this helps!


    The way forward always starts with a minimal test.

      This is what i tried, and still not working. it just hangs on Processing...

      i first made sure that i get well structured json

      #!/usr/bin/perl -wT use strict; use warnings; use DBI; use JSON; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); my $host = "host"; my $usr = "user"; my $pwd = "pwd"; my $dbname = "datname"; my $dbh = DBI->connect("DBI:mysql:$dbname:$host", $usr, $pwd, { RaiseError => 1, }) or die $DBI::errstr; my $Test = $dbh->prepare("SELECT idnum, fname FROM dbuser"); $Test->execute(); my (@data,$idnum,$fname); foreach (@data = $Test->fetchrow_array()) { $idnum = $data[0]; $fname = $data[1]; } print "Content-type: text/html\n\n"; my $json = encode_json( { $idnum, $fname} ); print $json;

      OUTPUT IS

      {"748847":"Jane Deo"}

      And this is my html

      <!doctype html> <html> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.ne +t/v/bs-3.3.7/jq-3.3.1/dt-1.10.18/datatables.min.css"/> <script type="text/javascript" src="https://cdn.datatables.net/v/bs/jq +-3.3.1/dt-1.10.18/datatables.min.js"></script> <script type="text/javascript" src="https://stackpath.bootstrapcdn.com +/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script> <script> jQuery(function($){ $("#table_id").DataTable({ "processing": true, "serverSide": true, "ajax": "file.pl" }); }); </script> </head> <body> <h1>sample dataTable</h1> <table id="table_id" class="table table-hover"> <thead> <tr> <th>ID</th> <th>NAME</th> </tr> </thead> <tbody> <tr> <td></td> <td></td> </tr> <tr> <td></td> <td></td> </tr> </tbody> </table> </body> </html>
        {"748847":"Jane Deo"}

        Did you look at any of the DataTables examples? Consider the following:

        use DBI; use JSON::XS; my $host = "host"; my $usr = "user"; my $pwd = "pwd"; my $dbname = "datname"; my $dbh = DBI->connect("DBI:mysql:$dbname:$host", $usr, $pwd, { RaiseError => 1, }) or die $DBI::errstr; my $json; $json->{data} = $dbh->selectall_arrayref('SELECT idnum, fname FROM dbu +ser', {Slice => {}} ); warn encode_json( $json );
        print "Content-type: text/html\n\n";

        JSON isn't html: application/json, and why are you manually printing headers when you're using CGI which has a method for that?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11157691]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-05-21 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found