我希望通过ajax向Perl脚本发送数据,并从中接收json格式的返回。但它没有起作用。我知道以下脚本中有些问题。有人知道如何修复吗?
jQuery代码:
jQuery代码:
$("#test").click(function(){
var ID = 100;
var data = {
data_id : ID
};
$.ajax({
type: "POST",
url: "ajax.cgi",
data: data,
success: function(msg){
window.alert(msg);
}
});
});
ajax.cgi(Perl脚本):
#!/usr/bin/perl
use CGI;
use DBI;
$cgi = CGI->new;
# Here I'd like to receive data from jQuery via ajax.
$id = $cgi->param('data_id');
$json = qq{{"ID" : "$id"}};
$cgi->header(-type => "application/json", -charset => "utf-8");
print $json;
exit;
use strict; use warnings
以声明变量。(2) 据我所见,您实际上没有print
$cgi->header(...)
。(3) 要查看您的脚本收到了什么参数,请将参数转储到日志文件中:use Data::Dumper; print STDERR Dumper $cgi->Vars
。(4) 要对JSON进行解码或编码,请使用use JSON
模块。 - amon