Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!

I have this AJAX call to a non- CGI Perl script where I am trying to pass values to the Perl script. If I pass like this:
... $("#send").click(function(){ $.ajax({ type: 'POST', url: 'process.pl?first=<TMPL_VAR NAME="FR">&last=<TMPL_VAR NA +ME="LA">', dataType: "json", success: function(data) { if(data){ alert("We pass!"); } }, error: function() { alert("We have a problem!"); } }); }); ...

And expecting in the Perl script like this: process.pl
#!/usr/bin/perl use strict; use warnings; use CGI; my $q = new CGI; my $first = $q->param("first"); my $last = $q->param("last"); print "F=$first - L=$last\n"; exit;

I got nothing!

Passing like this from the AJAX:
... url: 'process.pl?<TMPL_VAR NAME="FR"> <TMPL_VAR NAME="LA">', ...

And in the Perl script:
... my ($first, $last ) = @ARGV; print "F=$first - L=$last\n"; ...

It will print from values in "$first", but "$last" is empty!
Any suggestions?

Thanks for looking!

Replies are listed 'Best First'.
Re: Passing values from an AJAX call to non-CGI Perl script
by haukex (Archbishop) on Dec 10, 2018 at 19:18 UTC

    There are several issues with the code you showed:

    • Your JS is expecting JSON (dataType: "json"), but you're just doing a print "F=$first - L=$last\n";.
    • In jQuery's .ajax function, you should give it the query string as the data parameter, instead of trying to encode it yourself.
    • You're not printing a HTTP header in the response.

    For your second piece of Perl code, note that CGI scripts don't usually get their data passed to them via @ARGV. I'm not sure what you mean by "non- CGI Perl script". Are you saying that you have two scripts, a CGI script and another script that it takes its arguments on the command line, and you're trying to call the latter from the former? If so, see my post on running external commands somewhat more safely here, but in any case you should be very careful with what user input you pass along. You probably also want to use Taint mode.

    The following works correctly for me:

    test.html:

    <!DOCTYPE html> <html lang="en-US"> <head> <title>Example</title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> <script> "use strict"; $(document).ready(function() { $("#send").click(function() { $.ajax({ type: 'POST', url: 'process.cgi', data: { first:'<TMPL_VAR NAME="FR">', last:'<TMPL_VAR NAME="LA">' }, dataType: "json", success: function(data) { if (data) { alert("We pass!"); } }, error: function() { alert("We have a problem!"); } }); }); }); </script> </head> <body> <div><button id="send">Send</button></div> </body> </html>

    process.cgi:

    #!/usr/bin/env perl use warnings; use strict; use open qw/:utf8 :std/; use CGI qw/header/; use JSON::MaybeXS qw/encode_json/; my $q = new CGI; my $first = $q->param("first"); my $last = $q->param("last"); my $response = { F=>$first, L=>$last }; print header(-charset=>'UTF-8', -type=>'application/json'); print encode_json($response);

    The resulting data variable in the JS code is { "F": "<TMPL_VAR NAME=\"FR\">", "L": "<TMPL_VAR NAME=\"LA\">" }.

    Update: Removed a stray set of parens from the text.

Re: Passing values from an AJAX call to non-CGI Perl script
by NetWallah (Canon) on Dec 10, 2018 at 17:13 UTC
    Tru URI-escaping the parameters like:
    %3Ffirst%3D%3CTMPL_VAR%20NAME%3D%22FR%22%3E%26last%3D%3CTMPL_VAR%20NAM +E%3D%22LA%22%3E
    If in perl, you coud use URI::Escape.

                    Memory fault   --   brain fried