【问题标题】:JSON from Perl to Javascript从 Perl 到 Javascript 的 JSON
【发布时间】:2014-05-07 02:50:21
【问题描述】:

我将一个 JSON 对象从我的 Perl 程序发送到一个 Javascript jQuery 代码段。 虽然我多年来一直在 PHP 中这样做,但有些东西不起作用。 我的 Perl 代码是这样做的:

@cards=(1,2,3,4,5);
@repairs=(6,7,8,9,10);
my $json->{"cards"} = \@cards;
$json->{"repairs"} = \@repairs;
print "Content-type: text/html", "\n\n";
my $json_text = to_json($json);
print $json_text;

现在我的 javaScript/jquery 段是:

$.post(perl_program_name,
    { 
     card_code:card_code,
    },
    function(object) {
    alert('alerting'+object);
    var cards=new Array;
    var cards=object.cards;
    alert(cards);
    ...

第一个警报显示如下:

{"cards":["1","2","3","4","5"],
 "repairs":["6","7","8","9","10"]
}

但第二个警报显示卡片的长度为 0。数组未分配给卡片。 Perl 代码不添加双引号。它必须是通过 CGI 网关传送的 JSON 的副产品。

当我运行一个独立的 Javascript 场景时,它可以工作。

但通过 CGI 时不会。

感谢您的帮助。

【问题讨论】:

    标签: jquery json perl post


    【解决方案1】:

    查看:What is the correct JSON content type?

    use strict;
    use warnings;
    
    use JSON;
    
    print "Content-type: application/json\n\n";
    
    my $json = {
        cards => [1..5],
        repairs => [6..10],
    };
    
    print to_json($json);
    

    【讨论】:

    • 谢谢一百万。这是罪魁祸首。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    相关资源
    最近更新 更多