使用AJAX从javascript调用Perl脚本

时间:2021-06-01 01:23:35

I want to call a perl script from javascript. I have tried this using AJAX:

我想从javascript调用perl脚本。我用AJAX试过这个:

function create_request(obj) {
  var req = \"id=\"+0;
  var req_http =new XMLHttpRequest();
  req_http.open(\"POST\", \"create_file.pl\", false);
  req_http.setRequestHeader(\"Content-type\", \"application/x-www-form-urlencoded\");
  req_http.send(req);
  req_http.onreadystatechange = function() {
   if (req_http.readyState == 4) {
     var resp=req_http.responseText;
     document.write(resp); // *** echoes the content of create_file.pl
   }
  }
 }

create_file.pl:

create_file.pl:

#!/usr/bin/perl
#
#
use strict;
use warnings;
use CGI;

my $cgi = CGI->new;

open FILE, ">>file.txt" or die $!;
print FILE "aaa";
close(FILE);

print $cgi->header('text/plain;charset=UTF-8');
print 0;

The perl script only creates a text file.

perl脚本只创建一个文本文件。

After javascript calls the perl script, the print of the returned result is the entire content of create_file.pl, and the file file.txt it is not created.

在javascript调用perl脚本之后,返回结果的打印是create_file.pl的整个内容,而不是创建文件file.txt。

3 个解决方案

#1


2  

This has nothing to do with AJAX

If you are getting the contents of your perl file returned from your AJAX call, what this means is that your web server is not configured to handle perl (Javascript is accessing the location correctly, or you'd get a 404).

如果您正在获取从AJAX调用返回的perl文件的内容,这意味着您的Web服务器未配置为处理perl(Javascript正确访问该位置,或者您获得404)。

As for how to do this, without knowing your server it's hard to say. Usually you need to set up a rule to pass requests to .pl files through to your CGI handler.

至于如何做到这一点,不知道你的服务器很难说。通常,您需要设置规则以将.pl文件的请求传递给CGI处理程序。

In lighttpd (what I use), that's:

在lighttpd(我使用的)中,那是:

$HTTP["url"] =~ "/cgi-bin/" {
      cgi.assign = ( ".pl" => "/usr/bin/perl" )
}

If you're not using lighttpd, I'd suggest taking a Google for your-server cgi and following the instructions to configure it to parse those files through perl.

如果您不使用lighttpd,我建议您使用Google作为您的服务器cgi,并按照说明将其配置为通过perl解析这些文件。

#2


2  

After javascript calls the perl script, the print of the returned result is the entire content of create_file.pl, and the file file.txt it is not created.

在javascript调用perl脚本之后,返回结果的打印是create_file.pl的整个内容,而不是创建文件file.txt。

Then either:

然后:

  • You are working on a local file system instead of through a webserver or
  • 您正在使用本地文件系统而不是通过Web服务器或
  • You have not configured your server to treat the Perl program as CGI instead of a static file.
  • 您尚未将服务器配置为将Perl程序视为CGI而不是静态文件。

#3


1  

When you receive the text of a Perl script back from your web server, and you were expecting the web server to execute the script, then what you have is misconfigured web server.

当您从Web服务器收到Perl脚本的文本,并且您希望Web服务器执行该脚本时,您所拥有的是错误配置的Web服务器。

You need to configure your web server so that it knows to execute files.

您需要配置Web服务器,以便它知道执行文件。

Typically one separates one's executables from one's static content. and then configures the web server to allow execution of the programs in the executables directory. The exact syntax of that configuration will depend on which web server you're running.

通常,一个人的可执行文件与一个人的静态内容分开。然后配置Web服务器以允许执行可执行文件目录中的程序。该配置的确切语法取决于您正在运行的Web服务器。

#1


2  

This has nothing to do with AJAX

If you are getting the contents of your perl file returned from your AJAX call, what this means is that your web server is not configured to handle perl (Javascript is accessing the location correctly, or you'd get a 404).

如果您正在获取从AJAX调用返回的perl文件的内容,这意味着您的Web服务器未配置为处理perl(Javascript正确访问该位置,或者您获得404)。

As for how to do this, without knowing your server it's hard to say. Usually you need to set up a rule to pass requests to .pl files through to your CGI handler.

至于如何做到这一点,不知道你的服务器很难说。通常,您需要设置规则以将.pl文件的请求传递给CGI处理程序。

In lighttpd (what I use), that's:

在lighttpd(我使用的)中,那是:

$HTTP["url"] =~ "/cgi-bin/" {
      cgi.assign = ( ".pl" => "/usr/bin/perl" )
}

If you're not using lighttpd, I'd suggest taking a Google for your-server cgi and following the instructions to configure it to parse those files through perl.

如果您不使用lighttpd,我建议您使用Google作为您的服务器cgi,并按照说明将其配置为通过perl解析这些文件。

#2


2  

After javascript calls the perl script, the print of the returned result is the entire content of create_file.pl, and the file file.txt it is not created.

在javascript调用perl脚本之后,返回结果的打印是create_file.pl的整个内容,而不是创建文件file.txt。

Then either:

然后:

  • You are working on a local file system instead of through a webserver or
  • 您正在使用本地文件系统而不是通过Web服务器或
  • You have not configured your server to treat the Perl program as CGI instead of a static file.
  • 您尚未将服务器配置为将Perl程序视为CGI而不是静态文件。

#3


1  

When you receive the text of a Perl script back from your web server, and you were expecting the web server to execute the script, then what you have is misconfigured web server.

当您从Web服务器收到Perl脚本的文本,并且您希望Web服务器执行该脚本时,您所拥有的是错误配置的Web服务器。

You need to configure your web server so that it knows to execute files.

您需要配置Web服务器,以便它知道执行文件。

Typically one separates one's executables from one's static content. and then configures the web server to allow execution of the programs in the executables directory. The exact syntax of that configuration will depend on which web server you're running.

通常,一个人的可执行文件与一个人的静态内容分开。然后配置Web服务器以允许执行可执行文件目录中的程序。该配置的确切语法取决于您正在运行的Web服务器。