【发布时间】:2014-04-23 17:59:39
【问题描述】:
我正在尝试使用 perl 通过 CGI 调用 JSON RPC 函数。但是出现以下错误。
错误:[对象对象
有人可以帮我解决这个问题吗?提前致谢。
Test.html
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function() {
$.ajax({
type: "POST",
url: "cgi-bin/server.cgi",
data: '{"version": "1.1", "method": "echo", "params" : ["Hello World"]}',
processData: false,
async: false,
timeout: 30000, // msec
contentType: "application/json",
dataType: "json",
success: function(response){
$("body").append( "<br>SUCCESS: " + response.version );
$("body").append( "<br>version: " + response.version );
$("body").append( "<br>result: " + response.result );
$("body").append( "<br>OK" );
},
error: function(response){
$("body").append( "ERROR: " + response );
$("body").append( "<br>" );
}
});
});
</script>
</head>
<body>
</body>
</html>
server.cgi
[root@ip-172-31-0-164 cgi-bin]# cat server.cgi #!/usr/bin/perl
use JSON::RPC::Server::CGI;
use strict;
my $server = JSON::RPC::Server::CGI->new;
$server->dispatch('MyApp')->handle();
MyApp.pm
[root@ip-172-31-0-164 cgi-bin]# cat MyApp.pm
#!/usr/bin/perl
package MyApp;
use base qw(JSON::RPC::Procedure); # Perl 5.6 or more than
use strict;
sub echo : Public { # new version style. called by clients
# first argument is JSON::RPC::Server object.
return $_[1];
}
sub sum : Public(a:num, b:num) { # sets value into object member a, b.
my ($s, $obj) = @_;
# return a scalar value or a hashref or an arryaref.
return $obj->{a} + $obj->{b};
}
1;
【问题讨论】:
标签: jquery ajax json perl json-rpc