Link to home
Start Free TrialLog in
Avatar of steviedeehook
steviedeehookFlag for South Africa

asked on

Trouble launching perl script from within a java script

Hi,
I'm trying to follow a tutorial / example http://www.linuxjournal.com/article/9159
When it gets to the stage where the java script is supposed to launch the perl script it seems to stop. But I can't figure out why or how to troubleshoot it.
Steven
function start_cid () {
 
  setInterval("update_cid()", 1000);
}
 
function update_cid () {
  var req;
  var xml;
  var panels;
  var count;
  var name;
  var div;
 
  req = get_from_server();
 
  clear_panels();
 
  xml = req.responseXML.getElementsByTagName("panels")[0];
 
  panels = xml.getElementsByTagName("panel");
 
  for (count=0 ; count < panels.length ; count++) {
    panel = panels[count];
 
    name = panel.getElementsByTagName("name")[0];
    name = name.firstChild.nodeValue;
 
    content = panel.getElementsByTagName("content")[0];
    content = content.firstChild.nodeValue;
 
    div = document.getElementById(name);
 
    div.style.display="block";
    div.innerHTML = "<b>" + name + ": </b>" + content;
 
    if (div.innerHTML == "") {
      div.style.display="none";
    }
  }
}
 
function get_from_server () {
  var req;
 
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
 
  req.open("GET", "/cgi-bin/cid.pl", false);
  req.send(null);
 
  return req;
}
 
function clear_panels () {
  for (count=1 ; count < 5 ; count++) {
    document.getElementById("phone" + count).innerHTML = "";
    document.getElementById("phone" + count).style.display="none";
  }
 
  return;
}

Open in new window

Avatar of nognew
nognew
Flag of United Kingdom of Great Britain and Northern Ireland image

Hello!
 can you launch your Perl script directly from a browser?
like:
http:///cg-bin/cid.pl 
and what happen when you call it like that?
Regards,
t.
Avatar of steviedeehook

ASKER

it wants to download it as a file.

As I understand the .js is run by the browser - on the clinet - but the .pl is run as a process on the server... it's looking up files that are not accessible to the clinet, and the clients do not have perl installed?

there is some modification to the suggested code that we made to get it to run...

I will post the pl below.

If we run the pl from the bash prompt it runs fine....

#!/usr/bin/perl -w
#
use DBI; $dbh = DBI->connect ("dbi:mysql:database=asteriskcdrdb;host=localhost;user=root;password=passw0rd") or die "Can't connect to database.\n";
 
print "Content-type: text/xml\n\n\n";
 
print "<panels>\n";
 
check_cid("/tmp/panels/cid");
 
print "</panels>\n";
 
exit;
 
sub  check_cid {
  my($dir) = @_;
  my(@a, $a, $file, $count, $top);
  local(*FILE, *DIR);
 
  opendir DIR, "/tmp/panels/cid";
  while ($file = readdir(DIR)) {
 
    if ($file eq ".") { next; }
    if ($file eq "..") { next; }
 
    open FILE, "/tmp/panels/cid/$file";
    chomp($line = <FILE>);
    close FILE;
 
    ($dir, $name, $number, $uid) = split("#", $line);
 
    $count++;
 
    if ($dir eq "IN") {
      $html = "Incoming call from $name ($number)";
    } else {
      $html = "Outgoing call from $name ($number)";
    }
 
    expire_call($uid);
 
    print <<EOF
<panel>
  <name>phone$count</name>
  <content>$html</content>
</panel>
EOF
;
 
  }
}
 
sub  expire_call {
  my($id) = @_;
  my($sth, $count);
 
  $sth = $dbh->prepare("select count(*) from cdr where
uniqueid=\'$id\'");
  $sth->execute();
 
  ($count) = $sth->fetchrow_array();
 
  if ($count) {
    unlink("/tmp/panels/cid/$id");
  }
}

Open in new window

that might mean your cgi-bin directory is not set as a executable directory in httpd.conf file.
Do you have
ScriptAlias /cgi-bin/ "/"
set properly?
Hi,
In httpd.conf itlooks right...
I've attached it below
# ScriptAlias: This controls which directories contain server scripts.
# ScriptAliases are essentially the same as Aliases, except that
# documents in the realname directory are treated as applications and
# run by the server when requested rather than as documents sent to the client.
# The same rules about trailing "/" apply to ScriptAlias directives as to
# Alias.
#
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
 
#
# "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
</Directory>

Open in new window

The reason why your browser trying to download perl script rather then produce its output, because this line in your script perl fails:
use DBI; $dbh = DBI->connect ("dbi:mysql:database=asteriskcdrdb;host=localhost;user=root;password=passw0rd") or die "Can't connect to database.\n";
 
The result of it failure is an output "Can't connect to database.\n"
Which does not allow browser to recognise its mime type.
Amend you script like in snip, so the first output will be:
print "Content-type: text/xml\n\n\n";

all the best,
t.
#!/usr/bin/perl -w
#
print "Content-type: text/xml\n\n\n";
 
use DBI; $dbh = DBI->connect ("dbi:mysql:database=asteriskcdrdb;host=localhost;user=root;password=passw0rd") or die "Can't connect to database.\n";
  
print "<panels>\n";
 
check_cid("/tmp/panels/cid");
 
print "</panels>\n";
 
exit;
 
sub  check_cid {
  my($dir) = @_;
  my(@a, $a, $file, $count, $top);
  local(*FILE, *DIR);
 
  opendir DIR, "/tmp/panels/cid";
  while ($file = readdir(DIR)) {
 
    if ($file eq ".") { next; }
    if ($file eq "..") { next; }
 
    open FILE, "/tmp/panels/cid/$file";
    chomp($line = <FILE>);
    close FILE;
 
    ($dir, $name, $number, $uid) = split("#", $line);
 
    $count++;
 
    if ($dir eq "IN") {
      $html = "Incoming call from $name ($number)";
    } else {
      $html = "Outgoing call from $name ($number)";
    }
 
    expire_call($uid);
 
    print <<EOF
<panel>
  <name>phone$count</name>
  <content>$html</content>
</panel>
EOF
;
 
  }
}
 
sub  expire_call {
  my($id) = @_;
  my($sth, $count);
 
  $sth = $dbh->prepare("select count(*) from cdr where
uniqueid=\'$id\'");
  $sth->execute();
 
  ($count) = $sth->fetchrow_array();
 
  if ($count) {
    unlink("/tmp/panels/cid/$id");
  }
}

Open in new window

WOW!
That worked! - half :(
Lol
In Firefox it seems to run fine, but in IE it runs half way....
It sees the files in the tmp directory, turns them into xml, but doesn't delete them.... and then doesn't seem to run again after that, it just displays old information untill you close the browser and try again.
Steven
ASKER CERTIFIED SOLUTION
Avatar of nognew
nognew
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Such a small thing!
It's working great!
Thanks for all the help!
Steven
Thanks so much for the help!
Steven