CGI Perl 脚本将 HTML(以及“内容类型:text/html”)字符串打印到浏览器,而不是渲染它。如何修复?

发布于 2024-08-29 05:52:28 字数 555 浏览 2 评论 0原文

我有一个 perl CGI 脚本,需要发回一些 HTML,

print qq^Content-type: text/html\n\n
<HTML>
<HEAD>
<TITLE>some title</TITLE>
...
...
...
...
^:

而不是在浏览器中看到渲染的 HTML,而是看到整个 HTML 以及纯文本中的标签和“内容类型”行。以下是浏览器中的外观 -

Content-type: text/html


    <HTML>
    <HEAD>
    <TITLE>some title</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#000000" VLINK="#000000" ALINK="#000000" BACKGROUND="" onLoad=document.forms[0].elements[0].focus();>

I have a perl CGI script that needs to send back some HTML

print qq^Content-type: text/html\n\n
<HTML>
<HEAD>
<TITLE>some title</TITLE>
...
...
...
...
^:

Instead of seeing the rendered HTML in the browser, I see the entire HTML along with the tags and the 'content-type' line in plain text. Below is how things look in the browser -

Content-type: text/html


    <HTML>
    <HEAD>
    <TITLE>some title</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#000000" VLINK="#000000" ALINK="#000000" BACKGROUND="" onLoad=document.forms[0].elements[0].focus();>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没︽人懂的悲伤 2024-09-05 05:52:28

看起来 HTTP 标头是在输出之前发送的。在此代码之前是否有任何 print 语句(可能在某个函数中)?

另外,尝试启用警告和限制(如果您关闭它们)。

Seems like HTTP-header is sent before the output. Do you have any print statements (possibly in some function) before this code?

Also, try enabing warnings and strictures (if you have them off).

一桥轻雨一伞开 2024-09-05 05:52:28

另外,由于 CGI 脚本可以在没有 Web 服务器的情况下调用,只需手动调用脚本(使用正确的参数/环境变量(如果它们对脚本很重要)并查看输出。如 eugene y 所示,请求标头必须是CGI 脚本的第一个输出供服务器获取。

Also, as CGI scripts can be called without a web server, just call the script manually (with the right parameters / environment variables if they matter to the script and look at the output. As indicated by eugene y, the request headers must be the first output from the CGI script for them to be picked up by the server.

冬天旳寂寞 2024-09-05 05:52:28

听起来好像您的服务器尚未配置为将某些文件类型识别为 cgi 可执行文件。假设您使用的是 Apache,将此行添加到您的 httpd.conf 中即可解决问题,尽管还有许多其他配置方法可以达到相同的效果:

AddHandler cgi-script .cgi .pl

您可能还需要将 ExecCGI 添加到您的域的选项列表。有关详细信息,请参阅 Apache 教程:使用 CGI 的动态内容

It sounds as if your server hasn't been configured to recognize certain file types as cgi executables. Assuming you are using Apache, adding this line to your httpd.conf will do the trick, although there are many other ways of configuring this to achieve the same effect:

AddHandler cgi-script .cgi .pl

You may also have to add ExecCGI to an options list for your domain. See Apache Tutorial: Dynamic Content with CGI for more information.

笨死的猪 2024-09-05 05:52:28

print "内容类型:text/html\n\n";
这告诉浏览器两个换行符之后的文档将是 HTML。您必须发送标头,以便浏览器知道接下来将出现什么类型的文档,并且您必须在标头和实际文档之间包含一个空行。

所以你需要在“Content-type: text/html\n\n”之后有一个空行

print "Content-type: text/html\n\n";
This tells the browser that the document coming after the two newlines is going to be HTML. You must send a header so the browser knows what type of document is coming next, and you must include a blank line between the header and the actual document.

So you need a blank line after "Content-type: text/html\n\n"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文