Not sure if you have solved it now, but maybe someone else stumbles over this question and wonders how it works.
, . , index.html html (,/var/www/html) perl script cgi-bin (,/var/www/cgi-bin). perl script! cgi /cgi -bin/ajax/stackCGI - , .
, Perl cgi, AJAX JSON: click, , Javascript Perl AJAX JSON: .
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 ID = 100;
$.ajax({
type: 'POST',
url: '/cgi-bin/ajax/stackCGI/ajax.pl',
data: { 'data_id': ID },
success: function(res) {
alert("your ID is: " + res.result);
},
error: function() {alert("did not work");}
});
})
})
</script>
</head>
<body>
<button id="test" >Testing</button>
</body>
</html>
ajax.pl
use strict;
use warnings;
use JSON;
use CGI;
my $cgi = CGI->new;
print $cgi->header('application/json;charset=UTF-8');
my $id = $cgi->param('data_id');
my $op = JSON -> new -> utf8 -> pretty(1);
my $json = $op -> encode({
result => $id
});
print $json;