user690182
user690182

Reputation: 279

Perl CGI script not returning content

today i am trying to write my first CGI program. so i have a HTML page, a JavaScript program that both work just fine when i run them locally. so, the next step is to run it on an Apache server (locally on MacOSX) and the first example would be the "Hello World" that also works fine.

the problem is when i am trying to display "Content-type: text/html" what i have written is :

#!/usr/bin/perl -wT

use     strict ;
use     warnings ;
use     diagnostics ;
use     CGI ;
use     CGI::Ajax ;

print "Content-type: text/html \n\n";


sub initialize_html 
{
    my  $html   =   <<HTML ;
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <style type="text/css">
        html    { height: 100% }
        body    { height: 100%; margin: 0px; padding: 0px }
        label   { font-size:9px; text-align:center; color:#222; text-shadow:0 0 5px #fff; font-family:Helvetica, Arial, sans-serif; }
      #map_canvas { height: 100% }
    </style>
    <title>Network Weathermap | hellas online</title>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript" src="js/network_weathermap.js"></script>
</head>
<body>
    <div id="map_canvas"></div>
</body>
</html>
HTML
    print $html . "\n" ;
}

#initialize_html() ;
sub parameterize_info_window
{
    return  "CGI \n" ;
}

my $cgi     =   new CGI() ;
my $ajax    =   new CGI::Ajax( describeInfoWindow   =>  \&parameterize_info_window) ;

print   $ajax->build_html($cgi, \&initialize_html) ;
$ajax->JSDEBUG( 1 ) ;

and returns : "No head/html tags, nowhere to insert. Returning javascript anyway"

how can i overcome this problem ? what is the cause of it ?

thank you

Upvotes: 0

Views: 1518

Answers (2)

Dave Sherohman
Dave Sherohman

Reputation: 46187

I'm not quite clear on your core issue here, but one of the problems is that, when you get a request which will be handled by CGI::Ajax, you should not send the HTTP headers (e.g., Content-Type) yourself first. When you print $ajax->build_html(...) it will take care of that automatically for you. By sending your own headers first, you're moving CGI::Ajax's headers into the response body, where they won't work correctly.

Upvotes: 1

David L.
David L.

Reputation: 2103

Your initialize_html subroutine does not send back the html of the page.

The line print $html . "\n" ; needs to be changed to return $html . "\n" ; .

Upvotes: 3

Related Questions