Jquery 为什么可以';我是否从Perl脚本返回json对象?
我正在做一个使用jQuery、AJAX、JSON和Perl的小项目。在后端,我使用Perl及其CGI模块返回JSON ojbectJquery 为什么可以';我是否从Perl脚本返回json对象?,jquery,json,perl,Jquery,Json,Perl,我正在做一个使用jQuery、AJAX、JSON和Perl的小项目。在后端,我使用Perl及其CGI模块返回JSON ojbect #!/usr/software/bin/perl5.8.8 use strict; use warnings; use CGI qw(:standard); use JSON; print header('application/json'); my $json; # eventually the json that will be returned will
#!/usr/software/bin/perl5.8.8
use strict;
use warnings;
use CGI qw(:standard);
use JSON;
print header('application/json');
my $json;
# eventually the json that will be returned will based on params,
# for now using this list as example.
$json->{"entries"} = [qw(green blue yellow red pink)];
my $json_text = to_json($json);
print $json_text;
我能够写上面的脚本,在
使用jQuery的get调用此脚本:
jQuery.getJSON("script.pl?something=whatever", function(data, status) {
console.log(data);
console.log(status);
},"json").error(function(jqXhr, textStatus, error) {
/* I am always ending up here. */
console.log(jqXhr);
console.log("ERROR: " + textStatus + ", " + error);
});
从上面的jQuery调用中,我本来希望得到一个JSON对象,但是我得到了整个Perl脚本。我知道我的内容类型设置正确,因为当我从命令行执行脚本时,我得到了以下信息:
Content-Type: application/json
有人能帮我弄清楚吗。多谢各位
相反,我得到了整个Perl脚本
服务器没有运行Perl脚本,而是将其作为纯文本提供
您需要阅读服务器的手册,了解如何为CGI配置它
相反,我得到了整个Perl脚本
服务器没有运行Perl脚本,而是将其作为纯文本提供
您需要阅读服务器手册,了解如何将其配置为CGI。正如其他人所指出的,您需要将web服务器配置为将perl脚本作为CGI运行。如果您的web服务器是Apache,并且您的CGI名为“json.CGI”,那么您应该在httpd.conf中添加一行如下内容: AddHandler cgi script.cgi 既然您使用的是jQuery,那么您应该阅读JSON pod中的以下内容:
If you want to write a modern perl code which communicates to outer
world, you should use "encode_json" (supposed that JSON data are
encoded in UTF-8).
在故事的其余部分使用“perldocjson”。正如其他人所指出的,您需要配置web服务器以将perl脚本作为CGI运行。如果您的web服务器是Apache,并且您的CGI名为“json.CGI”,那么您应该在httpd.conf中添加一行如下内容: AddHandler cgi script.cgi 既然您使用的是jQuery,那么您应该阅读JSON pod中的以下内容:
If you want to write a modern perl code which communicates to outer
world, you should use "encode_json" (supposed that JSON data are
encoded in UTF-8).
其余部分使用“perldoc JSON”。这是您的web服务器的配置问题。需要将其配置为以CGI的形式运行Perl脚本。您使用的是哪台服务器?建议您在AJAX调用中使用JSON服务之前,首先通过直接从浏览器访问URL来测试JSON服务。生活(或至少调试)更简单:)这是web服务器的配置问题。需要将其配置为以CGI的形式运行Perl脚本。您使用的是哪台服务器?建议您在AJAX调用中使用JSON服务之前,首先通过直接从浏览器访问URL来测试JSON服务。生活(或至少调试)更简单:)