我需要进行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
由于某些原因,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
相关文章:
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- D3在一个调用中绘制不同的SVG形状,没有可见性
- 如何从Java/scala调用js美化程序
- 如何调用这个匿名 JavaScript 函数
- 如何从模块链中调用函数.导出到节点中
- 我需要从php调用javascript或jquery
- Chrome开发工具(如何知道我在调用哪个javascript对象)
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 如何最好地处理Web应用程序中的数据验证:纯JavaScript代码或对熟悉的perl包的ajax调用
- 从 JavaScript 调用 Perl 脚本
- 通过 APACHE 服务器调用多个 perl 脚本
- 在perl脚本中调用uglifyjs (javascript函数)
- 当Javascript调用Perl脚本时,捕获OS异常
- 我需要进行Ajax调用并显示我的DB表的内容,我使用Perl CGI并试图通过javaScript调用Perl脚本
- 从JavaScript调用Perl脚本
- 通过AJAX调用Perl Script来打印文本文件的内容
- 在没有服务器的情况下从java脚本调用perl
- Node.js调用perl脚本并获得标准输出
- 使用 D3 Xhr 调用 Perl 脚本