使用$ .ajax type = post,我调用一个执行mysql select的.pl文件,该文件应返回整行数据;相反,只有第一个字段通过$ .ajax帖子输出。 index.html的代码:
window.onload = function() {
$.ajax({
url: 'http://server.name:1234/DASH/cgi-bin/getapplicationinfo.pl',
data: ({ ajaxAction: 'getDBInfo'}),
dataType: 'html',
type: 'POST',
//async: false,
success: function(DB_INFO) {
a = DB_INFO;
obj = JSON.parse(a);
pr_url = obj.DB_INFO;
alert(pr_url); //i want this to contain all elements separated by comma.
document.getElementById('divMessage').innerHTML=pr_url[0];
document.getElementById('pNewOrders').innerHTML=pr_url[1];
document.getElementById('pBounceRate').innerHTML=pr_url[2];
},
error: function(request, status, err) {
document.getElementById("divMessage").innerHTML="FAILED!";
}
});
}
好吧,ajax post调用getapplicationinfo.pl:
my $q = CGI::new();
#
#print "!--->".$q->param('ajaxAction')."<br />";
if ( $q->param('ajaxAction') eq "getDBInfo" ){
#print('ajaxAction = getDBInfo<br />');
my $return = {};
#grab info from db=tool_environments.
my $dbConn = DBI->connect("DBI:mysql:database=tool_db;host=chrdbud01.gcsc.att.com;port=3306",'id','pw',{'RaiseError' => 1}) || return(0);
my $getDBInfo = $dbConn->prepare("select pr_url from db_connections where application = 'BERZ';");
#grab info from db=BERZ.
my $dbConn2 = DBI->connect("DBI:mysql:database=BERZ;host=server.name;port=1234",'id','pw',{'RaiseError' => 1}) || return(0);
my $getDBInfo2 = $dbConn2->prepare("select companyName from bmt_tickets where ticketNumber = '346404427';");
#grab info from db=teams.
my $dbConn3 = DBI->connect("DBI:mysql:database=teams;host=chrdbud01.gcsc.att.com;port=3306",'id','pw',{'RaiseError' => 1}) || return(0);
my $getDBInfo3 = $dbConn3->prepare("SELECT uid, manageruid FROM employees WHERE firstname='David' AND lastname='Parkam';");
my $msgInfo;
$getDBInfo->execute or die "SQL Error: $DBI::errstr\n"; #info from tool_environments
$getDBInfo2->execute or die "SQL Error: $DBI::errstr\n"; #info from BERT
$getDBInfo3->execute or die "SQL Error: $DBI::errstr\n"; #info from C3
if ($@) { warn "ERROR: $@";}
my @infoArr;
while (my ($msgInfo) = $getDBInfo->fetchrow_array()) {
push(@infoArr, $msgInfo);
}
while (my ($msgInfo) = $getDBInfo2->fetchrow_array()) {
push(@infoArr, $msgInfo);
print "@row\n";
}
while (my ($msgInfo) = $getDBInfo3->fetchrow_array()) {
push(@infoArr, $msgInfo);
print "@row\n";
}
$return->{DB_INFO} = \@infoArr;
my $json = to_json($return);
#print $q->header('text/html');
print $json;
exit(0);
}
如您所见,我们遍历“$ getDBInfo-&gt; execute或die”创建的每个fetchrow_array。来自index.html。另请注意,在index.html中,我警告数组,然后一次访问一个元素。但是,为什么不使用select语句中的多个字段推送数组? [即我的$ dbConn3查询,它肯定应该返回2个字段]。
任何帮助将不胜感激!
答案 0 :(得分:1)
my ($msgInfo) = $getDBInfo3->fetchrow_array();
将获取返回列表的第一个元素,并将其放入$msginfo
。如果您想要一个值列表,请使用更多类似的内容:
my @msg = $getDBInfo3->fetchrow_array();
答案 1 :(得分:0)
这一行:
while (my ($msgInfo) = $getDBInfo->fetchrow_array()) {
返回一个数组但你只捕获第一个元素。以下是一些例子:
~/ruby/rails41-test >perl
use Data::Dump 'dd';
my $x = (1,2,3);
dd $x;
my ($y) = (1,2,3);
dd $y;
my @z = (1,2,3);
dd @z;
^D
3
1
(1, 2, 3)
有关详细信息,请参阅perldata。