User Tools

Site Tools


cgi_jquery

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);

    $FORM{$name} = $value;

}
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 hashes 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_"}
...

Also one way to initialize a hash:

my %rec_hash = ('name'=>$name, 'type'=>$type,'data'=>$data,'ttl'=>$ttl);
cgi_jquery.txt · Last modified: 2022/10/17 12:57 by admin