Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mojolicous::LitejQuery自动完成问题_Jquery_Perl_Autocomplete_Mojolicious - Fatal编程技术网

mojolicous::LitejQuery自动完成问题

mojolicous::LitejQuery自动完成问题,jquery,perl,autocomplete,mojolicious,Jquery,Perl,Autocomplete,Mojolicious,自动完成不起作用:整个方法是错误的还是我只犯了一些错误 #!/usr/local/bin/perl use warnings; use 5.014; use utf8; use Mojolicious::Lite; use DBI; my $dbh = DBI->connect( ... ) or die $DBI::errstr; my $table = 'my_table'; get '/input' => sub { my $self = shift; $se

自动完成不起作用:整个方法是错误的还是我只犯了一些错误

#!/usr/local/bin/perl
use warnings; use 5.014; use utf8;
use Mojolicious::Lite;
use DBI;
my $dbh = DBI->connect( ... ) or die $DBI::errstr;
my $table = 'my_table';

get '/input' => sub {
    my $self = shift;
    $self->render( 'input' );
};

get '/search_db' => sub {
    my $self = shift;
    my $col = $self->param( 'col' );
    my $sth = $dbh->prepare( "SELECT $col FROM $table" );
    $sth->execute();
    my $ref;
    while ( my $row = $sth->fetchrow_arrayref() ) {
        push @$ref, @$row;
    }
    $self->render( json => $ref );
};

app->start;

__DATA__
@@ input.html.ep
<!DOCTYPE HTML>
<html>
<head>
    <title>Test</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="/js_local/development-bundle/jquery-1.6.2.js"></script>
    <script src="/js_local/development-bundle/ui/jquery.ui.core.js"></script>
    <script src="/js_local/development-bundle/ui/jquery.ui.widget.js"></script>
    <script src="/js_local/development-bundle/ui/jquery.ui.position.js"></script>
    <script src="/js_local/development-bundle/ui/jquery.ui.autocomplete.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#vorname").autocomplete({
                source: '/search_db?col=vorname',
                minLength: 2
            });
        });
    </script>
</head>
<body>
<form>
    <table>
        <tr><td>Vorname:</td><td><input type="text" id="vorname" 
        name="vorname" autocomplete="off"/></td></tr>
        <tr><td>Nachname:</td><td><input type="text" id="nachname" 
        name="nachname" autocomplete="on" /></td></tr>
    </table><br />
    <input type="submit" value="OK"/>
</form>
</body>
</html>

我想我更进一步了:现在,在第二个字符之后,我得到了所有的名称作为一个选择出现。

在minLength后面有一个额外的逗号:

$(function() { 
    $("#vorname").autocomplete({ 
        source: '/search_db?col=vorname', 
        minLength: 2, 
    }); 
}); 
先试试吧

我找到了一个解决方案:

#!/usr/local/bin/perl
use warnings;
use 5.014;
use utf8;
use Mojolicious::Lite;
use DBI;

my $table = 'my_table';
my $dbh = DBI->connect( ... ) or die $DBI::errstr;

get '/eingabe' => sub {
    my $self = shift;
    $self->render( 'eingabe' );
};

get '/search_db/:col' => sub {
    my $self = shift;
    my $col = $self->param( 'col' );
    my $term = $self->param( 'term' );
    my $sth = $dbh->prepare( 
                  "SELECT DISTINCT $col FROM $table WHERE $col LIKE ?" 
              );
    $sth->execute( $term . '%');
    my $ref;
    while ( my $row = $sth->fetchrow_arrayref() ) {
            push @$ref, @$row;
    }
    $self->render( json => $ref );
};

app->start;

__DATA__
@@ eingabe.html.ep
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="/development-bundle/jquery-1.6.2.js"></script>
    <script src="/development-bundle/ui/jquery.ui.core.js"></script>
    <script src="/development-bundle/ui/jquery.ui.widget.js"></script>
    <script src="/development-bundle/ui/jquery.ui.position.js"></script>
    <script src="/development-bundle/ui/jquery.ui.autocomplete.js"></script>
    <script type="text/javascript">
        $( document ).ready( function() {
            var data = [];
            var form = document.forms[0];
            var formEls = form.elements;
            var elLen = formEls.length;
            for ( var i = 0; i < elLen; ++i ) {
                if ( formEls[i].type != 'submit' ) {
                    data.push( formEls[i].id );
                }
            }
            var dLen = data.length;
            for ( i = 0; i < dLen; i++ ){
                $( "#" + data[i] ).autocomplete({
                    delay: 100,
                    minLength: 2,                                 
                    source: '/search_db/' + data[i]
                });
            }
        });
    </script>
</head>
<body>
    <form>
        <table>
            <tr><td>Vorname:</td><td><input type="text" id="vorname" 
            name="vorname" autocomplete="off" autofocus="on" /></td></tr>
            <tr><td>Nachname:</td><td><input type="text" id="nachname" 
            name="nachname" autocomplete="on" /></td></tr>
        </table><br />
        <input type="submit" value="OK"/>
    </form>
</body>
</html>

我看不出你们在哪里申报$table?从命令行运行两个脚本我得到:

Global symbol "$table" requires explicit package name at mojo_test2.pl line 19.
mojo_test2.pl had compilation errors.

您是否在javascript控制台中看到任何错误?js文件的路径正确吗?好的,你的web服务器日志中显示了什么?你确定你从未访问过die$col;线对我来说,你的请求似乎在那一行被执行并终止了,所以你看到了代码500。另外,当Mojolicious死亡时,如果附加控制台输出或日志内容也会很好?当我移除die$col时;我得到了同样的错误。@sid\u com您确实需要显示服务器日志所在的位置。在示例中删除了逗号