我需要进行Ajax调用并显示我的数据库表的内容,我正在使用Perl CGI并尝试通过javaScript调用Perl脚本

时间:2016-11-01 19:13:32

标签: javascript jquery ajax perl cgi

由于某种原因,Web控制台在document.ready函数中给出了一个错误。它说无法识别语法错误未识别标识符

这是我的cgi脚本,其中有JS调用perl脚本TestAj.pl,它返回一个json,我试图通过使用alert函数来显示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脚本TestAj.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;

由于

1 个答案:

答案 0 :(得分:2)

你的问题在于

print <<END_HERE;

因为这会在代码中插入任何perl变量。由于你可能不想这样做,你要么必须逃避每个印记($,%,@)或只是将这一行改为:

print <<'END_HERE';

HTH 乔治