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

Dear wise monks,

it appears i'm running into an error with CGI::Application and CGI::Ajax.

CGI::Ajax says that it "checks to see if a header() method is available to the CGI object, and then uses it. If method() isn't available, it creates it's own minimal header."

This is all fine and dandy, but when i run my code (shown below), it returns this error:

No head/html tags, nowhere to insert. Returning javascript anyway

package Notes; use strict; use CGI::Carp qw[fatalsToBrowser]; use CGI::Ajax; # hope it plays ok with C::A... use base 'CGI::Application'; my $pjx = CGI::Ajax->new ( 'export' => \&print_input ); sub setup { my $self = shift; $self->mode_param( "m" ); # mode param in query string $self->start_mode( "main" ); # starting run mode $self->error_mode("fatal_error"); $self->tmpl_path("tmpl/"); # other run modes $self->run_modes ( 'main' => 'main', ); } ### main page of the website ### when linking back to it, it's easiest to use ### <a href="?">home</a> sub main { my $self = shift; my $q = $self->query; # the CGI object my $tmpl = $self->load_tmpl( 'main.tmpl' ); $pjx->build_html( $q, \$tmpl->output ); } sub fatal_error { my $self = shift; my $error = $@; return $error; } ## ajax functions sub print_input { my $input = shift; return ($input . " was the inputted value"); } 1;
HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>AJAX Test</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +"> </head> <body> Enter something: <input type="text" name="val1" id="val1" onKeyUp="export( ['val1'], ['resultdiv'] );"> <br> <div id="resultdiv"></div> </body> </html>

 

Is CGI::Application not returning header tags that CGI::Ajax likes?

Please enlighten me.

meh.

Replies are listed 'Best First'.
Re: Peculiar (but probably expected) CGI::Ajax and CGI::Application problem
by rhesa (Vicar) on Jul 24, 2006 at 22:32 UTC
    The error you mention is triggered in CGI::Ajax::insert_js_in_head, and it means that the necessary javascript can't be inserted into your html document.

    I think the error is caused by your passing a reference to the template output. The way I understand the CGI::Ajax docs, you should either pass a coderef, or a string, to build_html. The quick fix to your code is therefore:

    sub main { my $self = shift; my $q = $self->query; # the CGI object my $tmpl = $self->load_tmpl( 'main.tmpl' ); $pjx->build_html( $q, $tmpl->output ); # <-- no reference }
Re: Peculiar (but probably expected) CGI::Ajax and CGI::Application problem
by arkturuz (Curate) on Jul 25, 2006 at 09:29 UTC
    There is a show_javascript() method in CGI::Ajax which returns JavaScript code. I haven't tested it. It seems to me that way you can put JavaScript code anywhere you like, so you don't need a CGI object to deal with.

      that could definitely work from what i can tell, it could sure solve the issue of conflicting CGI models...

      meh.