【问题标题】:Perl CGI script outputs raw HTML even after explicitly configuring Content-Type即使在显式配置 Content-Type 之后,Perl CGI 脚本也会输出原始 HTML
【发布时间】:2019-09-05 18:08:44
【问题描述】:

让我的 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 开发者工具上的 Elements 选项卡时,由于某种原因,整个 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>

任何帮助将不胜感激。

【问题讨论】:

  • // other code ...中是否还有其他输出,或者print header("text/html")是脚本中遇到的第一个打印语句?可以从命令行运行脚本吗?
  • “当我在 Chrome 的开发者工具中检查元素选项卡时,由于某种原因,整个 HTML 被包裹在 &lt;pre&gt; 标记中,而该标记又被包含在另一个 HTML 文档中。” — 您正在检查 Chrome 生成的 HTML 文档,以显示它被告知是纯文本的内容。
  • Don't use the CGI module尤其是不要使用它的html generating functions
  • @mob 在print header("text/html") 之前有几个print redirect() 语句在if 语句内部,但除此之外,它是第一个print 语句。在它之前有一个sprintf 函数,用于格式化日期并将其存储在变量中。

标签: html apache perl cgi content-type


【解决方案1】:

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

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 标头(也许还有&lt;pre&gt; 标记)。
  2. 您实际上并没有在 CGI 环境中运行代码,并且有一些不寻常的 Web 配置会从您的程序中获取输出并将其嵌入到另一个网页中。

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

顺便说一句,我强烈建议您阅读最新版本的CGI module documentationCGI::Alternatives 页面中标题为HTML Generation functions should no longer be used 的部分,以了解有关更好方法的一些建议。

【讨论】:

  • 你是对的。有一个自定义模块导入引发错误。禁用它会导致 HTML 被渲染。
【解决方案2】:

下面的代码有什么问题吗? -

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

$ 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>

【讨论】:

  • 道歉。我习惯于使用 // 用于 cmets 的语言进行开发,所以我习惯性地在这里使用它们;我在问题中对它们进行了编辑以反映这一点。实际代码没有任何 cmets。我只是在此处添加它们以进行澄清。所以输出仍然是问题中提到的。
  • 然而,我已经证明您发布的内容与您的问题无关。我们无能为力
【解决方案3】:

试试这个:

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

至少对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-30
    • 2014-12-11
    • 2012-01-19
    • 2016-08-31
    • 1970-01-01
    • 2017-03-12
    • 2015-04-03
    相关资源
    最近更新 更多