While this question may be outdated, it could still be of interest to someone else browsing through similar topics.
To pass a JavaScript array to Perl via Ajax, one method is to convert the array into a JSON object using "JSON.stringify(jsArray);" and then decode it in the Perl script. Below is a simple example where the first item of the array is returned through an alert.
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Testing ajax</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#test").click(function(){
var jsArray = ["employee", "admin", "users", "accounts"];
var jsArrayJson = JSON.stringify(jsArray);
$.ajax({
type: 'POST',
url: '/cgi-bin/ajax/stackCGI/processJsArray.pl', //change the path
data: { 'searchType': jsArrayJson},
success: function(res) {alert(res);},
error: function() {alert("did not work");}
});
})
})
</script>
</head>
<body>
<button id="test" >Push</button>
</body>
</html>
processJsArray.pl
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use JSON;
my $q = CGI->new;
my @myJsArray = @{decode_json($q->param('searchType'))}; #read the json object in as an array
print $q->header('text/plain;charset=UTF-8');
print "first item:"."\n";
print $myJsArray[0]."\n";