简体   繁体   English

用jquery $ .ajax()调用perl CGI

[英]Calling perl CGI with jquery $.ajax()

I am trying to call a perl script with a .cgi extension through an ajax call with jQuery, but am not having much luck. 我正在尝试通过jQuery的ajax调用来调用扩展名为.cgi的perl脚本,但是运气不佳。 On the command line the application runs fine, but when calling it with JS in the browser after a click event nothing seems to be returned. 在命令行上,该应用程序运行良好,但是在单击事件后在浏览器中使用JS调用该应用程序时,似乎没有任何返回。

If someone could point out what's going wrong it would appreciated. 如果有人指出发生了什么问题,将不胜感激。

FYI permissions are set to 755. The apache2 handler is set for .cgi .pl .py .ppl and .perl so I know it's not a handler or permissions issue. FYI权限设置为755。apache2处理程序设置为.cgi .pl .py .ppl和.perl,因此我知道这不是处理程序或权限问题。

.js file .js文件

function getData() {

var data = $("#textBox").val(); // this contains a small string

    $.ajax({
        url:"/public_html/cgi-bin/emailSubList.cgi",
        type:"GET",
        data:"data="+data,
        async:false,
        success:function(res){
            alert("result is: " + res);
            }
       }); 
  }

.cgi perl script .cgi perl脚本

#!usr/local/bin/perl

use CGI;
use DBI;
use strict;

my $in = new CGI;
my $dataIn = $in->param('data');
#this connects, but I don't need to share the login to all
my $dbh = DBI->connect("DBI:mysql:database:username:password);
my $sth;
my $result;

$sth = $dbh->prepare("SELECT id FROM dataList WHERE data=?");
$sth->execute($dataIn);

my @res = $sth->fetchrow_array();

if(@res > 0) {
    $result = 'Data has already been submitted';
}
else {
$sth = $dbh->prepare("INSERT INTO dataList ($dataIn) VALUES (?)");
$sth->execute($dataIn);

$result = 'Data added!';
}

print $in->header('text/plain;charset=UTF-8');
print "$result";

UPDATE: there is an error being thrown, 500 internal server error, but I'm not seeing why since the permissions are 755 and the syntax checks out ok with perl -wc script_name.cgi any thoughts on how to debug this or other things to check for would be very helpful. 更新:有一个错误引发,500内部服务器错误,但是我不明白为什么原因是权限为755,并且语法使用perl -wc script_name.cgi可以正常进行检查,关于如何调试此或其他内容的任何想法检查将非常有帮助。

The above is called with a click event in a web page, but it doesn't seem to execute the perl app. 上面的操作在网页中带有click事件,但是似乎没有执行perl应用程序。 The goal here is to get data sent to a DB and return it, or something else, from the same DB with a simple ajax call. 这里的目标是通过一个简单的ajax调用将数据发送到DB并从同一DB返回它或其他。

Just noting some issues I can see in the script as it's posted. 只是注意一些我在脚本发布时可以看到的问题。 Sorry if these are things that were copy paste errors from posting to stackoverflow, rather than the core issues. 抱歉,如果这些都是复制错误,则从发布到堆栈溢出粘贴错误,而不是核心问题。

  1. Database connection string is missing a closing quote. 数据库连接字符串缺少右引号。

  1. The insert statement looks incorrect 插入语句看起来不正确

$sth = $dbh->prepare("INSERT INTO dataList ($dataIn) VALUES (?)");

should be: 应该:

$sth = $dbh->prepare("INSERT INTO dataList (data) VALUES (?)");

The OP has posted a comment explaining that the problem has been solved. OP发布了一条评论,解释该问题已解决。 As he doesn't seem to want to post an answer explaining it for other people who have similar problems, I'll do it. 由于他似乎不想发布答案来为有类似问题的其他人解释该答案,因此我会做。

If you're uploading files from Windows to a Unix/Linux server, then it's important to do that in ASCII mode. 如果要将文件从Windows上传到Unix / Linux服务器,则以ASCII模式执行此操作很重要。 That way, all of the Windows-style line-end characters will automatically be converted to Unix-style line-end characters. 这样,所有Windows风格的行尾字符都将自动转换为Unix风格的行尾字符。 If you don't do that, then when your file arrives on the server, it will appear to all be on one line (as the Windows-style line-end characters won't be recognised). 如果您不这样做,那么当文件到达服务器时,它将全部显示在一行上(因为Windows样式的行尾字符将无法识别)。

This is particularly important for programs which are executes using the shebang line (and, of course, Perl programs fall into that category). 这对于使用shebang行执行的程序尤其重要(当然,Perl程序属于该类别)。 The shebang line is supposed to include the path to the program which is used to execute the code. shebang行应该包含用于执行代码的程序的路径。 And if that line doesn't end you are very likely to get some very strange behaviour. 如果那条线没有结束,您很有可能会得到一些非常奇怪的行为。

It's worth adding a couple more points: 值得补充几点:

  • This problem will almost certainly have left a useful message in the web server error log. 这个问题几乎肯定会在Web服务器错误日志中留下有用的消息。
  • This is a good reason why it's well worth developing on the same platform that you will be using for deployment. 这是一个很好的理由,因此值得在将用于部署的同一平台上进行开发。 With good virtual machine environments available for free for all platforms (including Windows), there is no reason why you can't develop in Linux on your Windows machine. 有了适用于所有平台(包括Windows)的免费的良好虚拟机环境,您就没有理由不能在Windows计算机上的Linux中进行开发了。

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

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