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

hello all, 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</title> <script type="text/javascript" src="http://maps.google.com/maps/ap +i/js?sensor=false"></script> <script type="text/javascript" src="js/network_weathermap.js"></sc +ript> </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 => \&paramete +rize_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

Replies are listed 'Best First'.
Re: CGI ... newbie
by wind (Priest) on Apr 11, 2011 at 05:08 UTC

    Your problem is this line:

    print $html . "\n" ;

    Should be

    return $html . "\n";

    Just look at the example in CGI::Ajax more closely.

Re: CGI ... newbie
by Anonymous Monk on Apr 10, 2011 at 22:37 UTC
    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

    Here is how you figure it out, you add

    print "File ", __FILE__, " at line ", __LINE__, " calling build_html \ +n";
    and you watch to see where this diagnostic message shows up.
Re: CGI ... newbie
by locked_user sundialsvc4 (Abbot) on Apr 11, 2011 at 04:34 UTC

    You often have to debug experiments like this by looking in at least three places:

    1. The Apache server “access” and “error” logs.
    2. Anything that might be output to the OS/X “console” utility.
    3. The information gleaned from a client-side debugger such as Firefox’s FireBug plug-in, which allows you to delve into exactly what the server actually sent.

    Once you clearly see the problem, a resolution is usually fairly easy.   “Aye, but there’s the rub...”

    Now, obviously, “nobody in the real world actually does it” quite the way you’re doing it now, although I do applaud you for trying it this way first.   (Yours is a sort of Linux from Scratch approach, and I know that I learned more from that “sip from the firehose” than any book or class could possibly have taught ... and faster, too.)   There are many frameworks, including the venerable CGI::Application, and as you continue your quest you might find it useful to check-out some of the source code of those packages.   See what kind of high-level abstractions they offer to the application programmer, then see how they implemented them.