且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何使用jQuery blur事件一个Ajax请求Perl脚本?

更新时间:2023-11-21 08:45:46

您似乎已经通过在shell中运行它,用户编写的命令行脚本,设计为与互动。 (使用的Getopt是一个很大的线索在这里)。

为了拥有它你需要重写一遍,这样它会与网络服务器(而不是一个shell)的HTTP请求进行响应。

有几种方法可以做到这一点。一个简单的方法是使用CGI。现代的做法是使用普拉克,可能与一个框架相结合。

一个基本的介绍使用Perl / CGI和Apache在 Apache文档可用。你应该看看一个模块,如 CGI 为了处理输入数据,并正确地发出HTTP标头。

您可以了解更多有关普拉克从项目的主页,其中包括链接到一些框架使用它

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);
$cardnumber=$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.