使用CGI方法通过web服务器将参数从JavaScript传递到Perl脚本
Using CGI methods to pass a parameter from JavaScript to Perl script over a web server
明智的人:我正在构建的web应用程序仍然存在极端的问题。我使用的是APACHE 2网络服务器。在localhost中,单击test.html将弹出一个测试按钮,该按钮应在我的.html文件中运行javascript。
test.html如下:
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
var str, fileNameVar;
fileNameVar = "hello";
if (xmlhttp.readyState==4 && xmlhttp.status==200){
str = xmlhttp.responseText;
alert(str);
}
}
xmlhttp.open('GET', 'try.pl?name=fileNameVar' + encodeURIComponent(fileNameVar),false);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>Example</h2></div>
<button type="button" onclick="loadXMLDoc()">test</button>
</body>
</html>
这个web应用程序以前在调用perl脚本并打印其返回值时也工作过,但这次我试图将一个参数从javascript传递到我的perl脚本[try.pl]。正如你所看到的,我试图传递变量fileNameVar,它将包含一个字符串或一个整数。
这是我的perl脚本,try.pl:
#!C:/indigoampp/perl-5.12.1/bin/perl.exe
use CGI;
use strict;
use warnings;
my $cgi = CGI->new;
my $name = $cgi->param('name');
print "Content-type: text/plain'n'n";
print "$name";
因此,当在我的网络应用程序中按下按钮时,它只需向用户创建一个包含"你好"一词的警报。
不过,当我按下按钮时,什么也没发生。我不知道自己做错了什么,因为这似乎对别人有用。从本质上讲,我的web应用程序可以工作,但我想添加将变量从javascript传递到perl脚本的功能。
注意:我不想在GET语句中直接传递"hello"。我希望它传递存储在fileNameVar中的任何内容。不过,在本例中,fileNameVar被设置为"hello"。感谢您的帮助!
1)将test.html放在apache2的htdocs目录中。
2) 将try.pl放在apache2的cgi-bin目录中。
3) 使try.pl可执行(这可能是在Windows上自动完成的)。在unix上,您可以写:
.../apache2/cgi-bin$ chmod a+x try.pl
4) 启动apache
5) 通过在浏览器中输入以下内容来请求test.html页面:
http://localhost:8080/test.html
(在apache正在侦听的任何端口中替换)
6) 在行中:
xmlhttp.open('GET', 'try.pl?name=fileNameVar' + encodeURIComponent(fileNameVar),false);
你有一些问题。您在函数内部定义了fileNameVar,并试图从函数外部访问fileNameVar。函数中定义的变量只能在该函数中看到(称为"闭包"的东西除外,它是在其他函数中定义了的函数)。
其次,在您的示例中,encodeURIComponent()将返回"hello",因此您的url看起来像:
'try.pl?name=fileNameVar' + 'hello'
或等效地:
'try.pl?name=fileNameVarhello'
这可能不是你想要的。你想写:
var fileNameVar = "hello";
....'try.pl?name=' + encodeURIComponent(fileNameVar)
接下来,您需要将"cgi-bin"放在路径中:
var fileNameVar = "hello";
......'cgi-bin/try.pl?name=' + encodeURIComponent(fileNameVar)
7) 行中:
print "$name";
引号没有任何作用。
===
/apache2/htdocs/test.html:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var str = xmlhttp.responseText;
alert(str);
}
}
var fileNameVar = "hello"; //LOOK AT ME*******
xmlhttp.open(
'GET',
'cgi-bin/try.pl?name=' + encodeURIComponent(fileNameVar),
false
);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>Example</h2></div>
<button type="button" onclick="loadXMLDoc()">test</button>
</body>
</html>
apache2/cgi-bin/try.pl:
#!/usr/bin/env perl
use CGI;
use strict;
use warnings;
my $cgi = CGI->new;
my $name = $cgi->param('name');
print "Content-type: text/plain'n'n";
print $name;
要在浏览器中输入的url:
http://localhost:8080/test.html
(在您将apache配置为侦听的任何端口中替换——它在…/apache2/conf/httpd.conf中指定)
然后单击按钮。
"在localhost中,单击test.html"对我说,您可能正在使用文件url访问页面,这将阻止XMLHttpRequest工作。
您还希望使用标准的javascript库,而不是本机XMLHttpRequest调用。如今,世界上大部分地区都在使用jquery。
- 为什么我的javascript脚本在页面加载时运行,而不是使用onclick
- 如何知道是否加载了JavaScript(脚本)
- onclick'放在JavaScript脚本中时无法工作
- 直接导航到chrome中的页面时未加载Javascript脚本
- 我将如何在将数据发布到 php 脚本的 javascript 脚本中输入变量
- Javascript 和 AngularJS2.为什么 JavaScript 脚本过早触发并假设不起作用
- jquery和JavaScript脚本不起作用
- 两个javascript脚本的非独立性
- 从另一个Javascript脚本(Canvas3D)更新QML属性
- 仅当存在某些HTML元素时才运行多个Javascript脚本
- 只有在条件满足时才加载javascript脚本-不起作用
- 在没有JQuery的情况下加载HTML主体之后调用JavaScript脚本的最佳方式
- 如何将PHP截图插入Javascript脚本
- 如何在javascript脚本标记中使用剃刀for循环
- 有没有办法同时加载JavaScript脚本
- 使用 Angularjs 加载 JavaScript 脚本的最佳方法是什么?
- 进入页面时未知的javascript脚本向下滚动
- Java 脚本中的 JavaScript 脚本中的绑定方法调用
- 克隆整个 JavaScript 脚本引擎
- 将 PHP 变量发送到 JavaScript.<脚本>标记的位置