I want to call a perl script from javascript. I have tried this using AJAX:
function create_request(obj) {
var req = \"id=\"+0;
var req_http =new XMLHttpRequest();
req_http.open(\"POST\", \"create_file.pl\", false);
req_http.setRequestHeader(\"Content-type\", \"application/x-www-form-urlencoded\");
req_http.send(req);
req_http.onreadystatechange = function() {
if (req_http.readyState == 4) {
var resp=req_http.responseText;
document.write(resp); // *** echoes the content of create_file.pl
}
}
}
create_file.pl:
#!/usr/bin/perl
#
#
use strict;
use warnings;
use CGI;
my $cgi = CGI->new;
open FILE, ">>file.txt" or die $!;
print FILE "aaa";
close(FILE);
print $cgi->header('text/plain;charset=UTF-8');
print 0;
The perl script only creates a text file.
After javascript calls the perl script, the print of the returned result is the entire content of create_file.pl, and the file file.txt it is not created.