【发布时间】:2014-02-07 21:27:24
【问题描述】:
我试图理解 JSON RPC 的概念和它的 Perl 实现。虽然我可以找到很多 Python/Java 的示例,但在 Perl 中却发现很少或没有示例。
我正在关注this example,但不确定它是否完整。我想到的例子是添加 2 个整数。现在我设置了一个非常基本的 HTML 页面,如下所示:
<html>
<body>
<input type="text" name="num1"><br>
<input type="text" name="num2"><br>
<button>Add</button>
</body>
</html>
接下来,根据上面的例子,我有3个文件:
test1.pl
# Daemon version
use JSON::RPC::Server::Daemon;
# see documentation at:
# https://metacpan.org/pod/distribution/JSON-RPC/lib/JSON/RPC/Legacy.pm
my $server = JSON::RPC::Server::Daemon->new(LocalPort => 8080);
$server -> dispatch({'/test' => 'myApp'});
$server -> handle();
test2.pl
#!/usr/bin/perl
use JSON::RPC::Client;
my $client = new JSON::RPC::Client;
my $uri = 'http://localhost:8080/test';
my $obj = {
method => 'sum', # or 'MyApp.sum'
params => [10, 20],
};
my $res = $client->call( $uri, $obj );
if($res){
if ($res->is_error) {
print "Error : ", $res->error_message;
} else {
print $res->result;
}
} else {
print $client->status_line;
}
myApp.pl
package myApp;
#optionally, you can also
use base qw(JSON::RPC::Procedure); # for :Public and :Private attributes
sub sum : Public(a:num, b:num) {
my ($s, $obj) = @_;
return $obj->{a} + $obj->{b};
}
1;
虽然我了解这些文件各自的作用,但在组合它们并使它们一起工作时,我完全不知所措。
我的问题如下:
- HTML 页面中的按钮是否位于标签内(就像我们通常在基于 CGI 的程序中所做的那样)?如果是,那会调用什么文件?如果不是,那么如何传递要添加的值?
- 3 个 Perl 文件的执行顺序是什么?哪个叫哪个?执行流程如何?
- 当我尝试从 CLI(即使用 $./test2.pl)运行 perl 文件时,我收到以下错误:错误 301 永久移动。什么东西永久移动了?它试图访问哪个文件?我尝试从
/var/www/html和/var/www/html/test运行文件。
我们非常感谢您对理解其中细微差别的帮助。提前致谢
【问题讨论】:
标签: html json perl rpc json-rpc