即使在显式配置 Content-Type 之后,Perl CGI 脚本仍会输出原始 HTML

Perl CGI script outputs raw HTML even after explicitly configuring Content-Type

我在渲染 Perl CGI 脚本时遇到很多问题 HTML。它一直输出 HTML 作为纯文本。我什至尝试用 print header("text/html");

显式设置 Content-Type

下面的代码有没有问题? -

use CGI qw(:standard);

# other code

print header("text/html"); 
# also tried just print header;

my $banner = "Some text";
print start_html($banner);
print h3($banner);

print start_form(-method=>"POST");
# HTML form specific code
print end_form;

print end_html;

当我在 Chrome 的开发者工具上查看 元素 选项卡时,由于某种原因,整个 HTML 被包裹在 <pre> 中标签又位于另一个 HTML 文档中。所以 HTML 格式不正确,但我无法理解为什么 -

    <html>

    <head></head>

    <body>
        <pre style="word-wrap: break-word; white-space: pre-wrap;">&lt;!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    &lt;html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"&gt;
    &lt;head&gt;
    &lt;title&gt;Some text&lt;/title&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h3&gt;Some text&lt;/h3&gt;&lt;form method="post" action="/path/script.pl?param1=val1&param2=val2" enctype="multipart/form-data"&gt;

    //form specific code

&lt;/form&gt;
    &lt;/body&gt;
    &lt;/html&gt;</pre>
    </body>

    </html>

如有任何帮助,我们将不胜感激。

试试这个:

print(header(-type => 'text/html'));

至少对我有用。

Is there anything wrong with the following code? -

是的。对于初学者来说,它甚至无法编译。

$ perl <<'__EOS__'
use CGI qw(:standard);

// other code

print header("text/html"); // also tried just print header;
my $banner = "Some text";
print start_html($banner);
print h3($banner);

print start_form(-method=>"POST");
// HTML form specific code
print end_form;

print end_html;
__EOS__
Bareword found where operator expected at - line 3, near "// other"
        (Missing operator before other?)
Bareword found where operator expected at - line 3, near "other code"
        (Do you need to predeclare other?)
Bareword found where operator expected at - line 5, near "// also"
        (Missing operator before also?)
Bareword found where operator expected at - line 11, near "// HTML"
        (Missing operator before HTML?)
Bareword found where operator expected at - line 11, near "specific code"
        (Do you need to predeclare specific?)
syntax error at - line 3, near "// other "
syntax error at - line 5, near "// also tried "
syntax error at - line 11, near "// HTML form "
Execution of - aborted due to compilation errors.

但是在将所有 // 更改为 # 之后,我们得到了一个非常好的程序,它不输出任何 PRE 元素。

$ perl <<'__EOS__'
use CGI qw(:standard);

# other code

print header("text/html"); # also tried just print header;
my $banner = "Some text";
print start_html($banner);
print h3($banner);

print start_form(-method=>"POST");
# HTML form specific code
print end_form;

print end_html;
__EOS__
Content-Type: text/html; charset=ISO-8859-1

<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Some text</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h3>Some text</h3><form method="post" action="http://localhost" enctype="multipart/form-data"></form>
</body>
</html>

当我运行你的程序时,我得到以下输出:

Content-Type: text/html; charset=ISO-8859-1

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Some text</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h3>Some text</h3><form method="post" action="http://localhost" enctype="multipart/form-data"></form>
</body>
</html>

这正是我希望看到的,与您所看到的完全不同。我可以看到两种可能性来解释这种差异:

  1. 您程序的 # other code 部分中有些东西正在输出一组 CGI headers(也许还有 <pre> 标签)。
  2. 您实际上并没有 运行在 CGI 环境中编译代码,并且有一些不寻常的 Web 配置从您的程序中获取输出并将其嵌入到另一个网页中。

区分这两种情况的一种简单方法是 运行 从命令行访问您的程序并查看您获得的输出。如果你得到你的输出,那么问题出在 # other code 的某处,如果你得到我的输出,那么问题出在 Web 服务器配置中。

顺便说一句,我强烈建议您阅读标题为 HTML Generation functions should no longer be used in a recent version of the CGI module documentation and the CGI::Alternatives 页面的部分,了解一些关于更好方法的建议。