Reputation: 78
I am using AJAX and a web-based server [APACHE] to call a perl script.
I have my files in htdocs, where my server accesses these files. When I click on test.html it pops up a button "test" and successfully calls a perl script to simply print out a message. i.e. the perl script simply prints "helloworld" and the html file "alerts" the user, i.e. prints out "hello world" when the button is pressed. This works fine.
The problem is, what I want to do, is call a perl script "check.pl", where check.pl opens a text file "simple.txt", stores the content of this text file in a string and then prints the result. So by pressing the button that is generated by test.html, it should print out the contents of the text file. Right now simple.txt is simply one sentence.
Here is my HTML, it successfully executes a perl file [check.pl]:
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc() {
//create a variable that will reference the XMLHttpRequest we will create
var xmlhttp;
//****Want it compatible with all browsers*****
//try to create the object in microsoft and non-microsoft browsers
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//set the event to be a function that executes
xmlhttp.onreadystatechange=function() {
var a;
//when server is ready, go ahead
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
//get number from text file, add one to it and output
//the original number and the resulting number.
a = xmlhttp.responseText;
alert(a);
}
}
//execute perl script
xmlhttp.open("GET","check.pl",false);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<Apache2.2>/<check.pl>?fileName=<simple.txt>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
and here is the perl script it calls:
#test to see if we can open file and print its contents
#The following two lines are necessary!
#!C:\indigoampp\perl-5.12.1\bin\perl.exe
print "Content-type: text/html\n\n";
#This line allows the entire file to be read not just the first paragraph.
local $/;
#Open file that contains the source text to work with
open(FILESOURCE, "simple.txt") or die("Unable to open requested file: simple.txt :$!");
#Store the whole text from the file into a string
my $document = <FILESOURCE>;
print $document;
close (FILESOURCE);
I am new to perl, AJAX, HTML, and javascript. The issue is that when I press the button, nothing appears. When in fact, the contents of "simple.txt" should be alerted to the user. I looked at the error log file and it says "cannot open simple.txt, the file or directory does not exist". Though, as I stated before, all three of my files are in htdocs. What could be the issue here?
Upvotes: 1
Views: 4509
Reputation: 126722
I suspect the current working directory for your Perl script is different from htdocs
. You should fully-qualify the filename with its path.
Also:
You should always use strict
and use warnings
for every Perl program
As has been commented, the #!
line must be the first line in the file
You are telling the client that the following data is HTML when it is simple text.
You should use the three-parameter for of open
with lexical file handles.
This update of your program takes those points into account
#!C:\indigoampp\perl-5.12.1\bin\perl.exe
use strict;
use warnings;
my $filename = 'simple.txt';
open my $source, '<', 'C:\path\to\htdocs\\'.$filename
or die qq{Unable to open requested file "$filename": $!};
my @document = <$source>;
print "Content-type: text/plain\n\n";
print @document;
Upvotes: 1