I am trying to call a perl script with a .cgi extension through an ajax call with jQuery, but am not having much luck. On the command line the application runs fine, but when calling it with JS in the browser after a click event nothing seems to be returned.
If someone could point out what's going wrong it would appreciated.
FYI permissions are set to 755. The apache2 handler is set for .cgi .pl .py .ppl and .perl so I know it's not a handler or permissions issue.
.js file
function getData() {
var data = $("#textBox").val(); // this contains a small string
$.ajax({
url:"/public_html/cgi-bin/emailSubList.cgi",
type:"GET",
data:"data="+data,
async:false,
success:function(res){
alert("result is: " + res);
}
});
}
.cgi perl script
#!usr/local/bin/perl
use CGI;
use DBI;
use strict;
my $in = new CGI;
my $dataIn = $in->param('data');
#this connects, but I don't need to share the login to all
my $dbh = DBI->connect("DBI:mysql:database:username:password);
my $sth;
my $result;
$sth = $dbh->prepare("SELECT id FROM dataList WHERE data=?");
$sth->execute($dataIn);
my @res = $sth->fetchrow_array();
if(@res > 0) {
$result = 'Data has already been submitted';
}
else {
$sth = $dbh->prepare("INSERT INTO dataList ($dataIn) VALUES (?)");
$sth->execute($dataIn);
$result = 'Data added!';
}
print $in->header('text/plain;charset=UTF-8');
print "$result";
UPDATE: there is an error being thrown, 500 internal server error, but I'm not seeing why since the permissions are 755 and the syntax checks out ok with perl -wc script_name.cgi any thoughts on how to debug this or other things to check for would be very helpful.
The above is called with a click event in a web page, but it doesn't seem to execute the perl app. The goal here is to get data sent to a DB and return it, or something else, from the same DB with a simple ajax call.
The OP has posted a comment explaining that the problem has been solved. As he doesn't seem to want to post an answer explaining it for other people who have similar problems, I'll do it.
If you're uploading files from Windows to a Unix/Linux server, then it's important to do that in ASCII mode. That way, all of the Windows-style line-end characters will automatically be converted to Unix-style line-end characters. If you don't do that, then when your file arrives on the server, it will appear to all be on one line (as the Windows-style line-end characters won't be recognised).
This is particularly important for programs which are executes using the shebang line (and, of course, Perl programs fall into that category). The shebang line is supposed to include the path to the program which is used to execute the code. And if that line doesn't end you are very likely to get some very strange behaviour.
It's worth adding a couple more points: