我在 Ubuntu 16.04.1 LTS 上使用默认安装的 Apache 和 mod_perl,我还使用默认的 JSON::XS 复制了它,并从 CPAN JSON-XS-3.02 更新到最新版本。
如果我不使用 mod_perl,下面的代码适用于所有情况。
下面的脚本和 html 在通过 mod_cgi 与 POST 和 GET 请求一起使用 perl 时工作。
但是,如果我使用的是 mod_perl 并且我使用 POST(如提供的 html 中所示),它会失败,“Hello”不会打印,并且我在我的 apache 日志文件中收到以下错误。
用法:JSON::XS::new(klass)。
如果我通过 GET 方法传递相同的参数,则脚本可以正常工作。
测试2.pl
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use JSON::XS;
my $q = new CGI();
print $q->header(-type => 'text/plain');
my $action = $q->param('a');
my $json_str = '{"foo":"bar"}';
my $pscalar = JSON::XS->new->utf8->decode($json_str);
print "Hello";
exit 1;
调用上面的 HTML(在服务器上命名为 test2.pl)
<html>
<body>
<form action="test2.pl" method="POST">
<input type="text" name="a"/>
<button type="submit">
</form>
</body>
</html>