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

2023-09-10 14:02:57 作者:带风轻吻你

我要打电话给我的输入域的模糊功能的Perl脚本。但我真的不知道如何做到这一点,我找不到任何工作的事情与谷歌。我的$ C $的HTML页中c为

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org / TR / XHTML1 / DTD / XHTML1-transitional.dtd>
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8/>
<冠军> Guthaben anzeigen< /标题>
<链接相对=样式表的href =CSS / style.css文件类型=文本/ CSS/>
<脚本SRC =jQuery的-1.6.3.js类型=文/ JavaScript的>< / SCRIPT>

<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){

    $(#针)。模糊(函数(){
        警报($。阿贾克斯({
    键入:POST,
    网址:/cgi-bin/guthabentransfer.pl
数据:cardnu​​mber = 1234567890,
成功:函数(MSG){
警报(味精);
 }
 });
);
    });

  });
< / SCRIPT>

< /头>
<身体GT;
<! - < D​​IV CLASS =豪普特> - >
<形式的行动=/ aktivieren.pl的方法=邮报>
<表格边框=0>
&其中; TR>
        < TD align =left> Kaartnummer:LT; / TD>
        < TD align =left> <输入类=textfeldNAME =kartennummer类型=文本最大长度=19>< / TD>
        < TD align =left><输入ID =针级=针NAME =pinnr类型=文本最大长度=4值=PIN>< / TD>
    < / TR>
        &其中; TR>
        < TD align =left>余额:< / TD>
        < TD align =left> <输入ID =平衡级=textfeldNAME =kartennummer类型=文本最大长度=19>< / TD>

    < / TR>

< /表>

< / DIV>

< /形式GT;
< / DIV>
< /身体GT;
< / HTML>
 

这是我的Perl脚本,我认为这将是最简单的打印结果:/

 #!的/ usr / bin中/ perl的
要求cgi-lib.pl;
使用funktionen;
使用的Getopt ::龙;

&安培; GetOptions(cardnu​​mber:秒=> \ $ cardnu​​mber);
$ cardnu​​mber = $查询 - >参数('cardnu​​mber');
如果($ cardnu​​mber!=){
    打印和放大器; funktionen :: checkbalance($ cardnu​​mber);
}
 

解决方案 JQuery

您似乎已经通过在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.