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

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.

Replies are listed 'Best First'.
Re^6: Server-side processing?
by frank1 (Monk) on Feb 15, 2024 at 09:42 UTC

    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?

        why are you manually printing headers when you're using CGI which has a method for that?

        Good question. ISTM that the code provided by frank1 isn't actually using CGI.pm at all for anything. So I would suggest just keeping the explicitly printed header and ditch the loading of CGI.pm instead. A single header is as easy to print by hand as it is using the CGI header method.


        🦛

        HASH(0x55cf3f782d88)

        thats what i get