0

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

3
  • 1
    what returns "no head/html" - your browser? the console? your log files? what request are you making? are you hitting the url in your browser address bar, or making an actual ajax request in javascript? Please enlighten, I want to +1 this for perl :) Commented Apr 10, 2011 at 21:30
  • hi, thanx for the interest, when i type "localhost/cgi-bin/network_weathermap.cgi" in my browser address bar. Commented Apr 11, 2011 at 0:59
  • there is no actual ajax request in javascript, there is just a function named "describeInfoWindow" and i would like the function "parameterize_info_window" to be called. Commented Apr 11, 2011 at 1:02

2 Answers 2

3

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" ; .

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.