简体   繁体   English

Perl CGI不包含Javascript文件

[英]Perl CGI doesn't include Javascript file

It is a rather wierd problem. 这是一个比较棘手的问题。 Consider the following small perl code: 考虑以下小的perl代码:

#!/usr/bin/perl

use strict;
use warnings;
use CGI qw{ :standard };
use CGI::Carp qw{ fatalsToBrowser };
my $q = CGI->new;
print "Content-type:  text/html\n\n";
print "<head>\n";
print "<script src='/home/bloodcount/Desktop/pm.js' type='text/javascript'></script>\n";
print "</head>\n";
print "<body>\n";
print "<h1>Click any number to see its factors</h1>\n";
print "</body></html>";

It prints a very small html page and includes a jasvascript file. 它会打印一个非常小的html页面,并包含一个jasvascript文件。 The problem is that the javascript file isn't included. 问题是不包含javascript文件。 The "physical" copy is in the correct place. “物理”副本在正确的位置。 I thought that something may be wrong with the code I am generating so I copied the raw html which comes out if you run this file in the console which is: Content-type: text/html 我认为我生成的代码可能有问题,因此我复制了原始html,如果您在控制台中运行此文件,则该原始html是:Content-type:text / html

<head>
<script src='/home/bloodcount/Desktop/pm.js' type='text/javascript'></script>
</head>
<body>
<h1>Click any number to see its factors</h1>
</body></html>

I ran it in chrome and it worked perfectly. 我在chrome中运行它,效果很好。 The javascript file has exactly one line if code which is: 如果代码为以下内容,则javascript文件只有一行:

console.log("It works!");

Any ideas what may be causing this? 任何想法可能是什么原因造成的? Note: I know that the second code listing doesn't have !DOCTYPE. 注意:我知道第二个代码清单没有!DOCTYPE。

Since you are able to execute the CGI within your browser you must have a local web server running. 由于您能够在浏览器中执行CGI,因此必须运行本地Web服务器。 Your <script src='...'> path is likely unreachable from the browser due to a lack of access rights or the proper alias configured within your web server. 您的<script src='...'>路径可能由于缺少访问权限或Web服务器中配置的适当别名而无法从浏览器访问。

It works from the static file because the browser is then going though filesystem directly, so the JS file path name resolves. 它可以从静态文件运行,因为浏览器随后将直接通过文件系统,因此JS文件路径名得以解析。

You have to put the .js file somewhere that the web server knows about, and then formulate your src path correctly. 您必须将.js文件放在Web服务器知道的位置,然后正确地制定src路径。

Check your web server logs and documentation to see how to set up the proper access rights and/or aliases. 检查您的Web服务器日志和文档,以了解如何设置适当的访问权限和/或别名。 Note you probably do not want to expose ~/Desktop to the internet. 请注意,您可能不想将~/Desktop暴露给Internet。

As an example, if you are using Apache, see USERDIR , ACCESS CONTROL , ALIAS . 例如,如果您使用的是Apache,请参阅USERDIRACCESS CONTROLALIAS

After some tinkering I found the solution: 经过一番修补,我找到了解决方案:

Apache searches for scripts and files only in the folder for this website meaning that each website has one specific folder where you must put the scripts. Apache仅在该网站的文件夹中搜索脚本和文件,这意味着每个网站都有一个特定的文件夹,您必须在其中放置脚本。 The base folder path is: /var/www/ and from there on you must find your website. 基本文件夹路径为: /var/www/ ,从那里必须找到您的网站。

This means that when before the set path was: /home/bloodcount/Desktop/pm.js it actually searched for the path /var/www/home/bloodcount/Desktop/pm.js which didn't exist. 这意味着在设置路径之前是/home/bloodcount/Desktop/pm.js它实际上搜索的路径/var/www/home/bloodcount/Desktop/pm.js不存在。 It wasn't searching in the real desktop, nor was there a permission problem. 它不是在真正的桌面中搜索,也没有权限问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM