如何使用jQuery blur事件向Perl脚本发出AJAX请求?

时间:2022-08-25 17:52:12

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

我想在输入字段的blur函数中调用Perl脚本。但我真的不知道该怎么做,而且我也找不到谷歌的工作。我的html页面代码是

<!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 :/.

这是我的Perl脚本,我想它将是最容易打印结果的:/。

#!/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);
}

2 个解决方案

#1


3  

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).

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

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).

为了让它响应HTTP请求,您需要重写它,以便它可以与webserver(而不是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.

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

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.

Apache文档中提供了使用Perl/CGI和Apache的基本介绍。您应该查看像CGI这样的模块,以处理传入的数据并正确地发出HTTP报头。

You can find out more about Plack from the project's homepage, which includes links to a number of frameworks that use it.

你可以从这个项目的主页找到更多关于Plack的信息,它包含了一些使用它的框架的链接。

#2


1  

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);
};

#1


3  

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).

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

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).

为了让它响应HTTP请求,您需要重写它,以便它可以与webserver(而不是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.

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

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.

Apache文档中提供了使用Perl/CGI和Apache的基本介绍。您应该查看像CGI这样的模块,以处理传入的数据并正确地发出HTTP报头。

You can find out more about Plack from the project's homepage, which includes links to a number of frameworks that use it.

你可以从这个项目的主页找到更多关于Plack的信息,它包含了一些使用它的框架的链接。

#2


1  

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);
};