如何通过Ajax将数据发送到Perl脚本? [英] How to send data to Perl script via ajax?

查看:97
本文介绍了如何通过Ajax将数据发送到Perl脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过ajax将数据发送到Perl脚本,并从中接收回json格式.但这是行不通的.我知道以下脚本有问题.有人知道如何解决吗?

I want to send data to Perl script via ajax, and to receive a json format back from it. But it doesn't work. I know something is wrong in the following scripts. Does anyone know how to fix it?

jQuery代码:

$("#test").click(function(){
    var ID = 100;
    var data = {
        data_id : ID                                                                        
    };

    $.ajax({        
        type: "POST",
        url: "ajax.cgi",
        data: data,
        success: function(msg){
            window.alert(msg);
        }       
    });
});

ajax.cgi(Perl脚本):

ajax.cgi (perl script):

#!/usr/bin/perl

use CGI;
use DBI;

$cgi = CGI->new;

# Here I'd like to receive data from jQuery via ajax.
$id = $cgi->param('data_id');     
$json = qq{{"ID" : "$id"}};

$cgi->header(-type => "application/json", -charset => "utf-8");
print $json;

exit;

推荐答案

不确定现在是否解决了这个问题,但也许其他人迷惑了这个问题并想知道它是如何工作的.

Not sure whether you solved it by now but maybe someone else stumbles over this question and wonders how it works.

请在下面找到代码.如果要运行此代码,只需将index.html文件复制到html目录(例如/var/www/html),然后将perl脚本复制到cgi-bin目录(例如/var/www/cgi-bin).确保使perl脚本可执行!在下面的代码中,cgi目录位于/cgi-bin/ajax/stackCGI中-请相应地进行更改.

Please find the code below. If you want to run this code, just copy the index.html file to your html directory (e.g. /var/www/html) and the perl script to your cgi-bin directory (e.g. /var/www/cgi-bin). Make sure to make the perl script executable! In my code below, the cgi directory is in /cgi-bin/ajax/stackCGI - please change that accordingly.

我还添加了一个有关如何使用Perl cgi,AJAX和JSON的高级示例:点击.

I also added a slightly more advanced example on how to use Perl cgi, AJAX and JSON: click and also one more example on how to pass an array from Javascript to Perl via AJAX using JSON: click.

index.html

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Testing ajax</title> 
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


    <script>

            $(document).ready(function() {

                $("#test").click(function(){
                    var ID = 100;
                    $.ajax({
                            type: 'POST',
                            url: '/cgi-bin/ajax/stackCGI/ajax.pl',
                            data: { 'data_id': ID },
                            success: function(res) {

                                                        alert("your ID is: " + res.result);

                                                    },
                            error: function() {alert("did not work");}
                    });
                })

            })



        </script>
    </head>
    <body>

        <button id="test" >Testing</button>

    </body>
</html>

ajax.pl

#!/usr/bin/perl

use strict;
use warnings;

use JSON; #if not already installed, just run "cpan JSON"
use CGI;

my $cgi = CGI->new;

print $cgi->header('application/json;charset=UTF-8');

my $id = $cgi->param('data_id');    

#convert  data to JSON
my $op = JSON -> new -> utf8 -> pretty(1);
my $json = $op -> encode({
    result => $id
});
print $json;

这篇关于如何通过Ajax将数据发送到Perl脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆