Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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!

Replies are listed 'Best First'.
Re: CGI::Application returning json
by Corion (Patriarch) on Feb 22, 2013 at 07:46 UTC
    Compare in the browser what you receive from the JSON URL of your script against the static JSON data you are serving from a file.

Re: CGI::Application returning json
by gg48gg (Sexton) on Feb 21, 2013 at 22:49 UTC
    Turn on warnings. Any warnings?
      I did, there is no warnings, I just think that the way CGI::Application uses to send back json is different than how I would outside or not using CGI::App. Lost here!