我需要进行Ajax调用并显示我的DB表的内容,我使用Perl CGI并试图通过javaScript调用Perl脚本

I need to make an Ajax call and display the contents of my DB Table,I am using Perl CGI and trying to call Perl script through javaScript

本文关键字:Perl 调用 javaScript 脚本 CGI Ajax 显示 DB 我的      更新时间:2023-09-26

由于某些原因,web控制台在文档中给了我一个错误。准备功能。它说未捕获的语法错误未识别标识符

这是我的cgi脚本,它有JS调用perl脚本TestAj.pl返回json,我试图通过使用警报函数显示perl输出来获取javascript函数getAppointments()中的数据。但是我没有在警报中得到任何东西。

我是新的perl cgi和ajax所以请让我知道,如果我做了一些愚蠢的事情。仅供参考,我已经在我的本地机器上测试了perl脚本,它给了我所需的输出。这是我的cgi脚本,其中包含js和html

#!"c:'xampp'perl'bin'perl.exe"
use strict;
use CGI;

my $query = new CGI;

print $query->header( "text/html" );
print <<END_HERE;
<html>
  <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
    <script type="text/javascript">
$(document).ready(function () {
     getAppointments();
 });
function getAppointments(){
$.ajax({
        type: "GET",
        url: "/cgi-bin/TestAj.pl", // URL of the Perl script
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data){
        alert(data);
        }, // success
        error: function() {alert("did not work");}
      });
}
    </script>
    <title>My First CGI Script</title>
  </head>
  <body bgcolor="#FFFFCC">
    <input type="button" value="New" onclick="showDiv()" />
<div id="hideShow" style="display:none;">
<form id="new app" method="POST" action="/cgi-bin/testing.cgi">
<input type="text" name="Date">Date<br>
<input type="text" name="Time">Time<br>
<input type="text" name="Description">Description<br>
<input type="submit" value="Add">
<input type="button" value="Cancel" onclick="hideDiv()" />
</form>
</div>
<p>       </p>
<p>       </p>
<p>       </p>
<form id="search" action="">
<table>
<tr>
<td>
<input type="text" name="searchApp"><br>
</td>
<td>
<input type="submit" value="Search"><form>
</td>

<script type="text/javascript">
    function showDiv() {
   document.getElementById('hideShow').style.display = "block";
}
    function hideDiv() {
   document.getElementById('hideShow').style.display = "none";
}


</script>
</body>
</html>
END_HERE

perl脚本testa .pl如下:

#!"c:'xampp'perl'bin'perl.exe"
use strict;
use warnings;
use JSON; 
use DBI;
use CGI;
our @query_output;
our $row="";
our $dbh="";
our $sth="";
our $dbfile = "";
$dbfile = "sample.db";
our $dsn      = "dbi:SQLite:dbname=$dbfile";
our $cgi = CGI->new;
our $user     = "";
our $password = "";
#print "Content-type: application/json; charset=iso-8859-1'n'n";
$dbh = DBI->connect($dsn, $user, $password, {
   PrintError       => 0,
   RaiseError       => 1,
   AutoCommit       => 1,
   FetchHashKeyName => 'NAME_lc',
});
$sth = $dbh->prepare("SELECT * FROM appointment");
$sth->execute() or die $DBI::errstr;
while ( my $row = $sth->fetchrow_hashref ){
push @query_output, $row;
}
   #my ($id,$Date,$Time,$Description) = @row;
   #my $json={{"id":"$id","Date":"$Date","Time":"$Time","Description":"$Description"}}
   # return JSON string
    #print @query_output;
    print $cgi->header(-type => "application/json", -charset => "utf-8");
    print JSON::to_json('@query_output);
    #print $json;

谢谢

问题出在

print <<END_HERE;

,因为这会在代码中插入任何perl变量。由于您可能不想这样做,您要么必须转义每个符号($,%,@),要么简单地将这一行更改为:

print <<'END_HERE';

HTHGeorg