User Tools

Site Tools


cgi_jquery

This is an old revision of the document!


Here is an example of transmitting a form using jquery post to a perl cgi script.

$(document).ready(function() {

    var formData = $("form#xapian").serialize();
    var jsonData = JSON.stringify(formData);
    jQuery.post("/cgi-bin/jwl/getdata.cgi",jsonData,success=function(data,textStatus,jqXHR)
        {
            console.log("data returned raw "+data);
        });

Perl code:

#!/usr/bin/perl
use strict;
use JSON;
use Data::Dumper;
open OUT,">","/tmp/perl.out";
print "Content-Type: text/html; charset=windows-1251\n\n";
my $buffer;
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
my $json = decode_json($buffer);
print OUT "Splitting\n";
my @pairs = split(/&/, $json);
my $n=scalar(@pairs);
print OUT "THere are $n values\n";
print OUT Dumper(@pairs);
my %FORM;
foreach my $pair (@pairs) 
{
    my ($name, $value) = split(/=/, $pair);
    print OUT "name=$name value=$value\n";
    #    print "$name-$value\n";
    $value =~ tr/+/ /;
    $value =~ s/%5B//g;
    $value =~ s/%5D/ /g;
    #    $value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
    #$value =~ s/~!/ ~!/g;
    $FORM{$name} = $value;
    #    print "Name=$name"." value= ".$value;
    #print "\n";
    #print $buffer."\n";
}
print OUT "Printing form\n";
print OUT Dumper(%FORM);

Output (edited):

Splitting
THere are 24 values
Printing form
$VAR1 = 'query';
$VAR2 = '                    ';
$VAR3 = 'xSORT';
$VAR4 = '_OSORT_';
$VAR5 = 'xDB';
$VAR6 = '_ODB_';
$VAR7 = 'xTERMS';
$VAR8 = '_OTERMS_';
$VAR9 = 'xAND';
$VAR10 = '_OAND_';
$VAR11 = 'CK_imap.gmail-3.com.db';
$VAR12 = 'CK_imap.gmail-3.com.db';

Dump is weird. It dumps hashs as an array of pairs so this is what the hash really looks like:

{"query":""
"CK_imap.gmail-3.com.db":"CK_imap.gmail-3.com.db"
"xDB":"_ODB_"
"xSORT":"_OSORT_"}
cgi_jquery.1666003928.txt.gz · Last modified: 2022/10/17 12:52 by admin