in reply to Passing values from an AJAX call to non-CGI Perl script
There are several issues with the code you showed:
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.
|
|---|