Reputation: 1869
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.
Upvotes: 2
Views: 4668
Reputation: 3484
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.
Upvotes: 1
Reputation: 15023
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.
Upvotes: 2
Reputation: 943563
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:
Upvotes: 2