Test.pm#!/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.tmplpackage 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;
<!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"> </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>
In reply to CGI::Application returning json by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |