CGI Perl 脚本将 HTML(以及“内容类型:text/html”)字符串打印到浏览器,而不是渲染它。如何修复?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来 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).
另外,由于 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.
听起来好像您的服务器尚未配置为将某些文件类型识别为 cgi 可执行文件。假设您使用的是 Apache,将此行添加到您的 httpd.conf 中即可解决问题,尽管还有许多其他配置方法可以达到相同的效果:
您可能还需要将
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:
You may also have to add
ExecCGI
to an options list for your domain. See Apache Tutorial: Dynamic Content with CGI for more information.所以你需要在“Content-type: text/html\n\n”之后有一个空行
So you need a blank line after "Content-type: text/html\n\n"