Hi all, I've been looking at how to call a perl script using a $.ajax, but I can't seem to get it to properly work. What I want to do is call a perl script from my jQuery code, let the perl script do some processing and then return the results to my jQuery code. So what I have is this:
$.ajax({
'type': 'GET',
'url': '/home/jquery/test.pl',
'async': false,
'success': function(data) {
alert(data);
}
});
test.pl
#!/usr/local/bin/perl
use strict;
print "Content-Type: text/html\n\n";
print "Hello!\n";
So whenever my alert(data) goes, it just prints the 'test.pl' code, so test.pl doesn't actually execute. I want test.pl to execute, so how would I go about doing this?
I've also tried using xmlhttp, like so:
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', '/home/jquery/test.pl?file=' + encodeURI('blah.txt'), true);
xmlhttp.onreadystatechange = updatePage;
xmlhttp.setRequestHeader(
'Content-Type',
'application/x-www-form-urlencoded'
);
xmlhttp.send(null);
function updatePage() {
if (xmlhttp.readyState == 4) {
alert(xmlhttp.responseText);
}
}
So then my test.pl looks like:
#!/usr/local/bin/perl
use strict;
use CGI qw/param/;
use URI::Escape;
print "Content-Type: text/html\n\n";
my $file = param('file');
$file = uri_unescape($file);
print "hello\n$file\n";
Still prints just the code, and the perl scripts are executable because I can run them just fine from the command line... I have to be doing something wrong. Any help would be appreciated. Also, how do I put in the code tags since [CODE] doesn't seem to work?