Following along in my mod_perl2 notes, I wanted to document how to get CGI::Ajax working with mod_perl2. I hit a couple of snags along the way that are worth noting. First, the generated javascript for my functions was calling httpd? + vars, rather than my URI /modperl_handler/ajax? + vars. This was frustrating, but I determined that it was grabbing httpd from $0, so I changed it locally and the script worked after that. The second snag I hit was because I was instantiating my CGI module globally instead of locally, and I would get segfaults now and then. Instantiating it inside the handler was the right way to go. Here is a working example:
package AjaxTest;
use CGI;
use CGI::Ajax;
use Apache2::RequestRec();
use Apache2::RequestIO();
use Apache2::Const -compile => qw(OK);
sub handler {
my ($r) = @_;
my $cgi = new CGI; # had this outside the handler and was getting segfaults
# Have to redefine $0 for CGI::Ajax because it's used to call further URLs from
# javascript ajax functions. (otherwise it did "httpd?"...)
local $0 = $ENV{"REQUEST_URI"};
$0 =~ s/?.*//;
# Start Ajax stuff
my $pjx = new CGI::Ajax("test_ajax" => &test_ajax);
# Don't compress javascript (1 for user fcns only, 2 for all)
$pjx->JSDEBUG(1);
# Send stderr to web logs
$pjx->DEBUG(1);
print $pjx->build_html( $cgi, &base_page);
return Apache2::Const::OK;
}
sub base_page {
return "Ajax mod_perl testmod_perl 2.0.2 on apache 2.2.2 rocks! <p><div id="test">Change me</div><p>nn";
}
sub test_ajax {
my $time = time();
return "Test successful; $time<p>";
}
1;
Mod_perl2, apache2.2, and CGI::Ajax notes... | 1 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Mod_perl2, apache2.2, and CGI::Ajax notes...
Authored by: nat on
Tuesday, July 11 2006 @ 05:02 PM CDT
Here's the same thing using Template Toolkit...
TemplateTest/AjaxTemplateTest.pm:
package TemplateTest::AjaxTemplateTest;
use CGI;
use CGI::Ajax;
use Apache2::RequestRec();
use Apache2::RequestIO();
use Apache2::Const -compile => qw(OK);
use Template;
my $tconfig = {
INCLUDE_PATH => "/usr/site/www/www-test/modperl_handlers/TemplateTest/templates", # or list ref
POST_CHOMP => 1 # cleanup whitespace
};
my $template = Template->new($tconfig);
sub handler {
my ($r) = @_;
my $cgi = new CGI; # had this outside the handler and was getting segfaults
# Have to redefine $0 for CGI::Ajax because it's used to call further URLs from
# javascript ajax functions. (otherwise it did "httpd?"...)
local $0 = $ENV{"REQUEST_URI"};
$0 =~ s/\?.*//;
# Start Ajax stuff
my $pjx = new CGI::Ajax("test_ajax" => \&test_ajax);
# Don't compress javascript (1 for user fcns only, 2 for all)
$pjx->JSDEBUG(1);
# Send stderr to web logs
$pjx->DEBUG(1);
# Default main page
my $template_file = 'base_page.html';
my $htmloutput='';
my $vars={};
eval {
$template->process($template_file, $vars, \$htmloutput) || die $template->error();
print $pjx->build_html( $cgi, $htmloutput);
};
if ($@) {
warn "Error: $@\n";
return Apache2::Const::SERVER_ERROR;
}
return Apache2::Const::OK;
}
sub test_ajax {
my $time = time();
my $vars = {"time" => time()};
my $htmloutput='';
my $template_file = "test_ajax.html";
eval {
$template->process($template_file, $vars, \$htmloutput) || die $template->error();
};
if ($@) {
warn "Error: $@\n";
return Apache2::Const::SERVER_ERROR;
}
return $htmloutput;
}
1;
TemplateTest/AjaxTemplateTest.pm:
package TemplateTest::AjaxTemplateTest; use CGI; use CGI::Ajax; use Apache2::RequestRec(); use Apache2::RequestIO(); use Apache2::Const -compile => qw(OK); use Template; my $tconfig = { INCLUDE_PATH => "/usr/site/www/www-test/modperl_handlers/TemplateTest/templates", # or list ref POST_CHOMP => 1 # cleanup whitespace }; my $template = Template->new($tconfig); sub handler { my ($r) = @_; my $cgi = new CGI; # had this outside the handler and was getting segfaults # Have to redefine $0 for CGI::Ajax because it's used to call further URLs from # javascript ajax functions. (otherwise it did "httpd?"...) local $0 = $ENV{"REQUEST_URI"}; $0 =~ s/\?.*//; # Start Ajax stuff my $pjx = new CGI::Ajax("test_ajax" => \&test_ajax); # Don't compress javascript (1 for user fcns only, 2 for all) $pjx->JSDEBUG(1); # Send stderr to web logs $pjx->DEBUG(1); # Default main page my $template_file = 'base_page.html'; my $htmloutput=''; my $vars={}; eval { $template->process($template_file, $vars, \$htmloutput) || die $template->error(); print $pjx->build_html( $cgi, $htmloutput); }; if ($@) { warn "Error: $@\n"; return Apache2::Const::SERVER_ERROR; } return Apache2::Const::OK; } sub test_ajax { my $time = time(); my $vars = {"time" => time()}; my $htmloutput=''; my $template_file = "test_ajax.html"; eval { $template->process($template_file, $vars, \$htmloutput) || die $template->error(); }; if ($@) { warn "Error: $@\n"; return Apache2::Const::SERVER_ERROR; } return $htmloutput; } 1;TemplateTest/templates/base_page.html
TemplateTest/templates/test_ajax.html