I am a complete newbie to Perl & Javascript/Jquery/Ajax. As an example, I'd like to send the string var exampleString
to test.pl, and the script will then write the string to file.
function sendToScript{
var exampleString = 'this is a string';
$.ajax({
url: './test.pl',
data: exampleString,
success: function(data, textStatus, jqXHR) {
alert('string saved to file');
}
}
test.pl
#!/usr/bin/perl -w
use strict;
#How do I grab exampleString and set it to $string?
open (FILE, ">", "./text.txt") || die "Could not open: $!";
print FILE $string;
close FILE;
Any help would be much appreciated.
You probably want something like
and test.pl
Here is an example using the Mojolicious framework. It can be run under CGI, mod_perl, PSGI or its own built-in servers.
save that to a file (say
test.pl
) and the run./test.pl daemon
which will start the internal server.Basically it sets up two routes, the
/
route is the user-facing page which runs the javascript request. The/save
route is the one that the javascript posts the data to. The controller callback appends the full post body to the file and then sends a confirmation message back which is then displayed by the success javascript handler.