How do I make an AJAX request to Perl script using jQuery blur event?
I want to call an Perl script in the blur function of my input field. But i dont really know how to do this, and i cant find any working things with google. my code of the html page is
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guthaben anzeigen</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<script src="jquery-1.6.3.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#pin").blur(function(){
alert($.ajax({
type: "POST",
url: "/cgi-bin/guthabentransfer.pl",
data: "cardnumber=1234567890",
success: function(msg){
alert(msg);
}
});
);
});
});
</script>
</head>
<body>
<!-- <div class="haupt"> -->
<form action="/aktivieren.pl" method="post">
<table border="0">
<tr>
<td align="left">Kaartnummer:</td>
<td align="left"> <input class="textfeld" name="kartennummer" type="text" maxlength="19"></td>
<td align="left"><input id="pin" class="pin" name="pinnr" type="text" maxlength="4" value="PIN"></td>
</tr>
<tr>
<td align="left">Balance:</td>
<td align="left"> <input id="balance" class="textfeld" name="kartennummer" type="text" maxlength="19"></td>
</tr>
</table>
</div>
</form>
</div>
</body>
</html>
This is my Perl script, i thought it will be the easiest to print the result :/.
#!/usr/bin/perl
require "cgi-lib.pl";
use funktionen;
use Getopt::Long;
&GetOptions("cardnumber:s" =>\$cardnumber);
$card开发者_StackOverflownumber=$query->param('cardnumber');
if ($cardnumber != "") {
print &funktionen::checkbalance($cardnumber);
}
You appear to have written a command line script, designed to be interacted with by a user running it in a shell. (The use of Getopt is a big clue here).
In order to have it respond to an HTTP request you need to rewrite it so that it will work with a webserver (instead of a shell).
There are several ways to do this. A simple approach would be to use CGI. A modern approach would be to use Plack, possibly in conjunction with a framework.
A basic introduction to using Perl/CGI with Apache is available in the Apache documentation. You should look at a module such as CGI in order to process incoming data and emit HTTP headers correctly.
You can find out more about Plack from the project's homepage, which includes links to a number of frameworks that use it.
That worked for me, call it in the blur event:
function perlExecute(name){
XMLHttp.open("GET", "/cgi-bin/balance.pl?cardnumber="+name, true);
XMLHttp.onreadystatechange = function() {
if (XMLHttp.readyState == 4) {
$("#balance").val(XMLHttp.responseText);
}
}
XMLHttp.send(null);
};
精彩评论