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.
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.
Share Improve this question edited Feb 26, 2014 at 19:29 Len Jaffe 3,4841 gold badge23 silver badges30 bronze badges asked Feb 26, 2014 at 14:31 georgiana_egeorgiana_e 1,87910 gold badges35 silver badges56 bronze badges 1- 2 Ignoring AJAX entirely, what happens if you call the perl script in your web browser? – Glitch Desire Commented Feb 26, 2014 at 14:41
3 Answers
Reset to default 2This has nothing to do with AJAX
If you are getting the contents of your perl file returned from your AJAX call, what this means is that your web server is not configured to handle perl (Javascript is accessing the location correctly, or you'd get a 404).
As for how to do this, without knowing your server it's hard to say. Usually you need to set up a rule to pass requests to .pl
files through to your CGI handler.
In lighttpd (what I use), that's:
$HTTP["url"] =~ "/cgi-bin/" {
cgi.assign = ( ".pl" => "/usr/bin/perl" )
}
If you're not using lighttpd, I'd suggest taking a Google for your-server cgi
and following the instructions to configure it to parse those files through perl.
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.
Then either:
- You are working on a local file system instead of through a webserver or
- You have not configured your server to treat the Perl program as CGI instead of a static file.
When you receive the text of a Perl script back from your web server, and you were expecting the web server to execute the script, then what you have is misconfigured web server.
You need to configure your web server so that it knows to execute files.
Typically one separates one's executables from one's static content. and then configures the web server to allow execution of the programs in the executables directory. The exact syntax of that configuration will depend on which web server you're running.