【发布时间】:2011-05-10 01:09:52
【问题描述】:
我正在努力找出失败的地方。我可以在我的本地 PC 上使用 php/jquery 让这个插件工作,但尝试在我的生产机器上使用 Perl 复制它(由于我不会进入的原因,php 不是一个选项)。
index.cgi - 这是生成包含文本框的 html 页面的 perl 代码。
#!/usr/bin/perl -w
use DBI;
use CGI;
use warnings;
use strict;
$cgi = new CGI;
$cgi->autoEscape(undef);
print $cgi->header;
print $cgi->start_html(-title=>'test',
-dtd=>'//W3C//DTD HTML 4.01 Transitional//EN',
-style=>'/themes/ui-lightness/jquery.ui.all.css',
-script=>[
{-type=>'javascript', -src=>'/js/jquery-1.5.2.min.js'},
{-type=>'javascript', -src=>'/js/test.js'},
{-type=>'javascript', -src=>'/ui/jquery-ui-1.8.11.custom.js'},
{-type=>'javascript', -src=>'/ui/jquery.ui.core.js'},
{-type=>'javascript', -src=>'/ui/jquery.ui.widget.js'},
{-type=>'javascript', -src=>'/ui/jquery.ui.position.js'},
{-type=>'javascript', -src=>'/ui/jquery.ui.autocomplete.js'}
]
);
print $cgi->start_div({-class=>'ui-widget'});
print $cgi->textfield(-id=>'customer',-size=>25),$cgi->br;
print $cgi->end_div(),$cgi->br;
print $cgi->div({-class=>'ui-widget-content',-id=>'log'});
print $cgi->end_html;
test.pl - 这是在后台运行以将 JSON 提供给自动完成的代码:
#!/usr/bin/perl
use warnings;
use strict;
use CGI;
use DBI;
use JSON;
my $cgi = CGI->new;
print $cgi->header(-type => "application/json", -charset => "utf-8");
my $dbh = DBI->connect('dbi:mysql:hostname=test;database=test',"test","test");
my $term = $cgi->param('term');
my $sth = $dbh->prepare(qq{SELECT customer.name, customer.id FROM test WHERE customer.name ?;}) or die $dbh->errstr;
$sth->execute($term.'%') or die $sth->errstr;
my $json = {};
while(my @customer = $sth->fetchrow_array()) {
$json->{$customer[0]} = $customer[1];
}
print JSON::to_json($json);
test.js - 这是实际使用的 JQuery:
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" ).attr( "scrollTop", 0 );
}
$( "#customer" ).autocomplete({
source: "test.pl?term=",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Value: " + ui.item.value + " Key " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
我一直在谷歌周围,但没有找到任何可靠的东西,有人有一个带有 JQuery 的 Perl 示例。 test.js 和 index.cgi 文件几乎完全复制了 jquery-ui 示例文件中用于 jquery 自动完成的代码,除了 index.cgi 是使用 CGI.pm 用 Perl 编写的。
任何帮助都将不胜感激,由于服务器的性质和其上的应用程序,我在这里的语言有些限制。
【问题讨论】:
-
test.pl 甚至无法编译。
-
我知道你说过你不会进入它,但我真的很好奇为什么你不能在你的服务器上安装 PHP
-
use strict和warnings丢失。不要忽略这些来浪费时间。 -
相信我,有一家价值数十亿美元的公司设法使用 Perl 和 jQuery 作为他们的 CGI 和界面,每天数百万次。当然,Perl 必须先编译。您的
$dbh分配需要一个分号。 USUW(“使用严格;使用警告;”) -
@Axeman 缺少的分号出现在我的环境中的代码中。我编辑了诸如用户名和密码之类的东西,在格式化这篇文章的代码时我可能不小心。 .cgi 和 .pl 文件按预期编译和运行,但不会填充自动完成功能。我不确定如何测试 JQuery 部分以查看它是否收到结果。我已经看到了 php 示例中使用的变量 'term',我将尝试使用 LWP,看看是否可以复制 $_GET 操作,看看是否有帮助。