Hi Monks!
I am building an autocomplete code using CGI::Application with jQuery/json. The jQuery code works outside of CGI::Application but trying the same functionality within CGI::Application is not working and as you can see in my test code I tried returning the values in many ways but it just doesn’t work. Any help showing how to do this will be very appreciated. Here is my test code with my issue:

test.pl
#!/usr/bin/perl use strict; use CGI::Carp qw(fatalsToBrowser); use lib 'MyLib'; use Test; my $webapp = App::Test->new(tmpl_path => 'templates/'); $webapp->run();
Test.pm
package App::Test; use strict; use base 'CGI::Application'; use CGI::Application::Plugin::JSON ':all'; use DBI; use JSON; my $db = 'DBServer'; my $user = 'user'; my $pass = 'passw'; my $mysql_dbh = DBI->connect( "DBI:ODBC:$db", $user, $pass ) || print "Connect fail: $!"; # Define run modes sub setup { my $self = shift; $self->start_mode('show_page'); $self->error_mode('error'); # The query parameter rm will contain the run mode name in run_mod +es $self->mode_param('rm'); $self->run_modes( 'show_page' => 'show_page', 'add_stuff' => 'add_stuff', ); } # Process any fatal errors sub error { my $self = shift; my $error = shift; return "There has been an error: $error"; } sub show_page { my $self = shift; #my $sql = "select TOP 5 * from my_table where account <>''"; #my $show = $self->_get_data($sql); my $template = $self->load_tmpl('test.tmpl'); #$template->param('data', $show); return $template->output(); } sub add_stuff { my $self = shift; #my $account = shift; my $q = $self->query; my $account = $q->param('term'); my $sql = qq{select name, account FROM master_broker where account li +ke ?}; my $data_handle = $mssql_dbh->prepare($sql); $data_handle->execute($account.'%')|| die $data_handle->errstr;; my @query_output; while ( my $row = $data_handle->fetchrow_hashref ){ push @query_output, $row; } # JSON OUTPUT $self->header_add(-type=>"application/json"); #print JSON::to_json(\@query_output); #return $self->json_body(\@query_output); my $json = to_json( \@query_output, {utf8 => 1} ); $json =~ s/"(\d+?)"/$1/g; # to_json puts quotes around numbers, we tak +e them off #$self->header_add(-type=>"application/json"); #return $json; my $template = $self->load_tmpl('test.tmpl'); $template->param('data', $json); return $template->output(); } 1;
test.tmpl
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=is +o-8859-1" /> <title>jQuery UI Autocomplete - Perl Example</title> <link rel="stylesheet" type="text/css" href="/ajax/libs/jquery +ui/1/themes/redmond/jquery-ui.css"> <script src="/ajax/libs/jquery/1/jquery.min.js"></script> <script src="/ajax/libs/jqueryui/1/jquery-ui.min.js"></script> <style type="text/css"> body {padding-top: 60px;} </style> <script type="text/javascript"> $(function() { $('#name').val(""); $('#account').autocomplete({ source: function( request, response ) { $.ajax({ url: "test.pl", dataType: "json", data: {term: request.term}, success: function(data) { response($.map(data, function(item) { return { label: item.name, value: item.account }; })); } }); }, minLength: 4, select: function(event, ui) { $('#name').val(ui.item.label); } }); }); </script> </head> <body> <div class="navbar navbar-fixed-top"> <div class="navbar-inner"> <div class="container"> <a class="btn btn-navbar" data-toggle="collapse" data-targ +et=".nav-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </a> </div> </div> </div> <div class="container"> <div class="hero-unit"> <p>Test</p> <tr><form class="" id="autocompleteForm" name="autocompleteForm" me +thod="post"> <fieldset> <td align="right">&nbsp;</td> <td align="center"> <input name="account" id="account" type="text" class=""> </td> <td align="center"> <input readonly="readonly" name="name" id="name" type="text" +size="50" class=""> </td> <td> <table border="1" cellspacing="0" cellpadding="0"> <tr> <td><input type="hidden" id="form_submitted" name="form_su +bmitted" value="true" /> <input name="btnSave" type="button" value="Add Order" +> </td> </tr> </table> </td> </tr></fieldset></form> <p id="submitted"></p> </div> </div> </body> </html>

Thanks again for the help!

In reply to CGI::Application returning json by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.