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

问题描述 投票:-1回答:3

我在使用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的Developer工具上检查Elements选项卡时,由于某种原因,整个HTML被包装在<pre>标签内,而 <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> 标签又在另一个HTML文档中。所以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>

任何帮助将不胜感激。

html apache perl cgi content-type
3个回答
2
投票

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

# other code

这正是我期望看到的,而不是你所看到的。我可以看到解释这种差异的两种可能性:

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

区分这两种情况的一种简单方法是从命令行运行程序,看看你得到了什么输出。如果你得到你的输出,那么问题出在HTML Generation functions should no longer be used的某个地方,如果你得到我的输出,那么问题出在Web服务器配置中。

顺便说一句,我强烈建议您阅读最新版本的CGI module documentationCGI::Alternatives页面中标题为$ 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. 的部分,以了解更好方法的一些建议。


0
投票

以下代码有什么问题吗? -

是。对于初学者来说,它甚至都没有编译。

//

但是在将所有#更改为$ 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> 之后,我们得到了一个完美的程序,它不会输出任何PRE元素。

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

-3
投票

试试这个:

qazxswpoi

至少那对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.