如何通过JavaScript发送JSON并在Perl中解码?

时间:2015-11-05 21:14:30

标签: json ajax perl

我正在尝试使用JavaScript发布JSON并使用Perl脚本读取POST结果。我编写了这段代码但是无法让Perl脚本读入JSON文本。

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>
var d = {
    "name": "Bob",
    "sex": "Male",
    "address": {
        "city": "San Jose",
        "state": "California"
    },
    "friends": [
        {
            "name": "Alice",
            "age": "20"
        },
        {
            "name": "Laura",
            "age": "23"
        },
        {
            "name": "Daniel",
            "age": "30"
        }
    ]
};

$(document).ready(function() {
    $("#test").click(function() {
        $.ajax({
            type: 'POST',
            url: '/cgi-bin/Raghav_test/Apollo/read_ajax3.pl',
            data: "r=" + d,
            success: function(res) { alert("data" + res); },
            error: function() { alert("did not work"); }
        });
    });
});
</script>
</head>
<body>
<button id="test">Testing</button>
</body>
</html>

的Perl:

#!/usr/bin/perl -w

use CGI;
#use DBD;
use DBI;
use JSON::PP;
use Data::Dumper;
use DBD::Oracle qw(:ora_types);
use lib "/var/www/cgi-bin/ICE_LIBRARY/";

require '/var/www/cgi-bin/import_scripts/library/common_lib.pl';
require "/var/www/cgi-bin/import_scripts/library/script_log.pl";

use database_conf;
my $db = new database_conf;

#my $EP_dev_conn = $db->db_eportal_dev;
my $EP_prod_conn = $db->db_eportal_prod;
my $cgi = CGI->new;
my $id = $cgi->param("r");
#my $data = $cgi->param('POSTDATA');

print "Content-type:text/html\n\n";

#my $value =  $ddata->{'address'}{'city'} ;
# Here I'd like to receive data from jQuery via ajax.
#my $id = $cgi->param('apiKey');
#$json = qq{{"ID" : "$id"}};
#my $method = $cgi->param('method');
#my $ip = $cgi->param('ip');

$json = qq{"$id"};
print $json;
exit;

1 个答案:

答案 0 :(得分:1)

在提出请求之前,您需要在对象上调用JSON.stringify()

$.ajax({
    type: 'POST',
    url: '/cgi-bin/Raghav_test/Apollo/read_ajax3.pl',
    data: { "r": JSON.stringify(d) },
    success: function(res) { alert("data" + res); },
    error: function() { alert("did not work"); }
});

然后你需要在字符串上调用decode_json()来解析它并将其转换为Perl数据结构:

my $q    = CGI->new;
my $json = $q->param("r");
my $href = decode_json($json);

print Dumper($href);

输出:

$VAR1 = {
          'address' => {
                         'state' => 'California',
                         'city' => 'San Jose'
                       },
          'name' => 'Bob',
          'friends' => [
                         {
                           'name' => 'Alice',
                           'age' => '20'
                         },
                         {
                           'age' => '23',
                           'name' => 'Laura'
                         },
                         {
                           'name' => 'Daniel',
                           'age' => '30'
                         }
                       ],
          'sex' => 'Male'
        };