UPDATE: Got it working. See readmore for code.

#!C:/xampp/perl/bin/perl.exe -w use strict; use CGI; use CGI::Ajax; use Template; # for template toolkit support use CGI::Carp qw[fatalsToBrowser]; #DEBUG ONLY my $config = { INCLUDE_PATH => 'tt/', # or list ref }; my $cgi = CGI->new; my $pjx = CGI::Ajax->new( 'login' => \&do_login ); my $tt = Template->new($config); print $pjx->build_html($cgi, \&main_page); sub do_login { my $input = shift; # do something with $input my $output = $input . " was the input!"; return( $output ); } sub main_page { my $template = 'main.tt'; my $vars = { title =>'AJAX Test' }; my $output = ''; $tt->process($template, $vars, \$output) || die $tt->error(), "\n"; return $output; }

Hey gang.

This is probably something that's right in front of my face, but I'm attempting to get started on some AJAX using CGI::Ajax and Template (to handle HTML processing). The demo from the docs ran great, however my code is throwing this error:

malformed header from script. Bad header=<html><head><s +cript type="text: ajax.cgi

Not only that, but it's returning the HTML as text to the browser.

Here's my code:

#!C:/xampp/perl/bin/perl.exe -w use strict; use CGI; use CGI::Ajax; use Template; # for template toolkit support use CGI::Carp qw[fatalsToBrowser]; #DEBUG ONLY my $config = { INCLUDE_PATH => '/tt', # or list ref INTERPOLATE => 1, # expand "$var" in plain text POST_CHOMP => 1, # cleanup whitespace PRE_PROCESS => 'header', # prefix each template EVAL_PERL => 1, # evaluate Perl code blocks }; my $cgi = CGI->new; my $pjx = CGI::Ajax->new( 'login' => \&do_login ); my $tt = Template->new(); print $pjx->build_html($cgi, \&main_page); sub do_login { my $input = shift; # do something with $input my $output = $input . " was the input!"; return( $output ); } sub main_page { my $template = "main.tt"; return $tt->process($template, { title =>'AJAX Login'}) or die $tt-> +error; }

Any ideas? I'm thinking it's something to do with how build_html in CGI::Ajax tries to cope with process in T::T.

Thanks in advance!

meh.

In reply to CGI::Ajax + Template::Toolkit Question by stonecolddevin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.