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.


In reply to Re: Passing values from an AJAX call to non-CGI Perl script by haukex
in thread Passing values from an AJAX call to non-CGI Perl script by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.