Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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

by msinfo (Sexton)
on Sep 01, 2013 at 13:36 UTC ( #1051796=perlquestion: print w/replies, xml ) Need Help??
msinfo has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,

i am getting below message in my web pages, which use CGI::Ajax module

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

Here is skeleton of my code:

my $pjx = new CGI::Ajax( 'exported_func' => \&perl_func); print $pjx->build_html($cgi, \&Show_Html); sub perl_func { # ajax function } sub Show_Html { # html generating code # first check session info print $session->header(); # generate html print $cgi->start_html(); # remaining html code print $cgi->end_html(); }

Now this generates, html properly and even calls Ajax function, but displays above warning message in browser, all the time.

I went through CGI::Ajax documentation, and below are the lines, which I doubt are related to problem. Arguments: The CGI object, and either a coderef, or a string containing html. Optionally, you can send in a third parameter containing information that will get passed directly to the CGI object header() call.

or is it mandatory to write Show_Html as follows only

sub Show_HTML { my $html = <<EOT; <HTML> # html generating code </HTML> EOT return $html; }

While doing debugging with Firefox, I found out that actual response body was as follows

# html generated by Show_Html <html xmlns="" lang="en-US" xml:lang="en-U +S"> <head> <title></title> </html> # and again html generated by cgi::ajax <html> </html>

Replies are listed 'Best First'.
Re: CGI::Ajax No head/html tags, nowhere to insert. Returning javascript anyway.
by poj (Prior) on Sep 01, 2013 at 16:03 UTC
    The Show_HTML sub should return the html, not print it.
    It is not mandatory to use a here-doc, you could built it up like this ;
    sub Show_HTML { my $html = $cgi->start_html(); $html .= $cgi->p('Enter something'); $html .= $cgi->input( {-type=>'text', -name=>"va1",-id=>"val1", onkeyup=>"exported_func( ['val1'], ['resultdiv'] );"} ); $html .= $cgi->br; $html .= $cgi->div( {-id=>"resultdiv"} ); $html .= $cgi->end_html(); return $html; }

      Thanks poj

      The Show_HTML sub should return the html, not print it

      I learned that from you.

      The error message goes away, when I use code format shown by you. But, when conditional statement is inserted, in between, it fails. Error says - problem at if condition. When I remove if condition, all goes well.

      my $html = $session->header(); $html .= $cgi->start_html(-charset=>'UTF-8',-style => {'src' => 'sty +lesheet/style.css'}); $html .= $cgi->div({-id=>'container'}, $cgi->img({src=>'images/pic.png', height=>'100px', + width=>'150px'}), if ($cgi->p(($session->is_expired) || ($session->i +s_empty)) =1) { $cgi->p("logged") }, $cgi->div(

        Doctor Doctor it hurts when I do this, but it doesn't hurt when I don't do it

        Ok then :)

        Put the conditional outside the div ;
        my $msg = ($session->is_expired || $session->is_empty) ? 'logged':''; $html .= $cgi->div({-id=>'container'}, $cgi->img({src=>'images/pic.png', height=>'100px',width=>'150px'}), $cgi->p($msg),$cgi->div(

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1051796]
Front-paged by Arunbear
[zentara]: Strange, on clicking a link in a node, I am taken to that node, but my votes and login are lost. Should I post a Permonks Discussion?
[choroba]: that just means someone used a wrong link format
[choroba]: msg them or consider to fix
[zentara]: Gratias

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2017-05-26 12:32 GMT
Find Nodes?
    Voting Booth?