简体   繁体   English

从启用JavaScript的网页将变量传递给Perl脚本

[英]Passing a variable to a Perl script from JavaScript-enabled web page

I am using HTML with JavaScript which calls a Perl script over an Apache 2 server. 我正在将HTML与JavaScript结合使用,该JavaScript通过Apache 2服务器调用Perl脚本。

I want to pass a variable from my JavaScript code to the Perl script it is calling. 我想将变量从我的JavaScript代码传递给它正在调用的Perl脚本。

I found a common way of doing this using CGI methods. 我发现了使用CGI方法执行此操作的常用方法。 It works successfully but is not quite what I want. 它可以成功运行,但不是我想要的。

For example, within my JavaScript I have this line: 例如,在我的JavaScript中,我有这行:

xmlhttp.open("GET", "try.pl?name=Joe", false);

which calls the Perl script, passing the parameter name to the script. 它将调用Perl脚本,并将参数name传递给脚本。

Inside my Perl script I have: 在我的Perl脚本中,我有:

#!C:/indigoampp/perl-5.12.1/bin/perl.exe
use CGI qw(:standard);
use strict;
use warnings;

my $query = new CGI;

my $name = $query->param('name');
print "Content-type: text/plain\n\n";
print "$name";

The JavaScript simply prints the result to the screen so Joe pops up. JavaScript只是将结果打印到屏幕上,从而弹出Joe

The problem is what I want to do is pass a variable. 问题是我想做的就是传递一个变量。 ie I have a variable called fileNameVar in my code which holds a string (the name of a file). 即我在代码中有一个名为fileNameVar的变量,其中包含一个字符串(文件名)。 I want to pass this variable to the Perl script. 我想将此变量传递给Perl脚本。

So I want something like: 所以我想要类似的东西:

xmlhttp.open("GET", "try.pl?name=fileNameVar", false);

My variable will be changing: I want it dynamic. 我的变量将会改变:我希望它是动态的。 I do not want to hard code the filename into the GET statement as they did with name=Joe , but how do I do this? 我不想像使用name=Joe那样将文件名硬编码到GET语句中,但是我该怎么做?

When I try it simply prints fileNameVar instead of what is stored in fileNameVar . 当我尝试时,它只打印fileNameVar而不是存储fileNameVar Any ideas? 有任何想法吗?

All I can find online is the literal ( name=Joe instead of name=variable ). 我在网上只能找到文字( name=Joe而不是name=variable )。 I am very new to web server concepts and any help would be greatly appreciated. 我对Web服务器概念非常陌生,任何帮助将不胜感激。

You should append an encoded version of the string to the URL without the query parameter value. 您应将字符串的编码版本附加到没有查询参数值的URL上。

A call to encodeURIComponent encodes as hex numbers characters that may otherwise be illegal within a URL. 调用encodeURIComponent会将字符编码为十六进制数字,否则这些字符在URL中可能是非法的。

The + operator concatenates strings. +运算符连接字符串。

So you want 所以你要

xmlhttp.open('GET', 'try.pl?name=' + encodeURIComponent(fileNameVar), false);

I don't think you can directly pass a javascript variable to a perl script. 我认为您不能直接将JavaScript变量传递给perl脚本。 You could save the contents of the javascript variable in a cookie and then read that cookie from within the perl CGI script. 您可以将javascript变量的内容保存在cookie中,然后从perl CGI脚本中读取该cookie。 Check this out: http://perldoc.perl.org/CGI/Cookie.html . 检查一下: http : //perldoc.perl.org/CGI/Cookie.html Or, you could set up a session to retain state between javascript and the perl script. 或者,您可以设置一个会话来保留javascript和perl脚本之间的状态。

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

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