1

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?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Amanda Kirk
  • 78
  • 2
  • 10
  • Try remove the first 3 lines. The script should start with the `#!C:\.....` line. – clt60 Apr 28 '13 at 19:01
  • `use strict;`, `use warnings;`, request the URI of the script directly so that you can see the output instead of seeing how JS handles the output, use the [three argument form of open](http://stackoverflow.com/questions/1479741/why-is-three-argument-open-calls-with-autovivified-filehandles-a-perl-best-pract). – Quentin Apr 28 '13 at 20:20

1 Answers1

1

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;
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • thanks everyone, i looked into the link noted which covered why using lexical file handles and the 3 parameter version of open was better and actually learned how to use that version. I have only worked with perl on simple file processing and am glad to know [I will go edit many of my scripts now]. Also, apologies for forgetting strict and warnings [the golden rule]. I also made #! the first line. May I ask what you meant by "You are telling the client that the following data is HTML when it is simple text."? – Amanda Kirk Apr 29 '13 at 22:19
  • I am also new to the terminology for AJAX and JS and using a web server for this type of procedure so I am not sure what is meant by "request the URI of the script directly so that you can see the output instead of seeing how JS handles the output". Thanks for all of the information. It is VERY helpful for my learning. I am going to run the script shortly and see how it goes. Will update. THANK YOU. – Amanda Kirk Apr 29 '13 at 22:22
  • Also I wanted to upvote you but it says I need a 15 reputation first. If there is anything I can do for your reputation let me know. The given script works perfectly! From there I got so much more done with my project for work. Thank you so much! – Amanda Kirk Apr 29 '13 at 23:00
  • @Amanda Kirk: What I meant is that you are sending an HTTP `Content-type` header of `text/html`, which says that the body of the message is HTML. It is actually plain text so your header should say `text/plain`. What I believe Quentin meant is that you should try accessing the URL to your Perl script directly instead of indirectly by running JavaScript code that loads it using `xmlhttp.open`. – Borodin May 01 '13 at 20:23