A mod_perl handler 

package JsAutocomplete;

use strict;
use warnings;

use Apache2::RequestRec;
use Apache2::Const -compile => qw(OK);

use Apache::DBI;
use JSON::XS     qw(encode_json);

sub handler : method {
    my($class, $r) = @_;

    my $self = $class->new(request => $r);
    my $ref  = $self->handle_request();    # error handling omitted

    my $body = encode_json($ref);
    $r->content_type('application/json');
    $r->headers_out->set('Content-Length' => length($body));
    $r->headers_out->set('Cache-Control' => 'private');

    $r->print($body);

    return Apache2::Const::OK;
}