2

im having a setback, im making an Html page with javascript, and im using AJAX to call Perl functions. The thing is when my Perl program doesn't need parameters the calling is trivial. But i have a function to open and read a file so i need to give the location of the file to the perl script, thus having to passe it trough a paramenter in the AJAX calling. Ex working call:

function getOption(){
   var selectmenu=document.getElementById("Select1")
    selectmenu.onchange=function(){  //run some code when "onchange" event fires
    var chosenoption=this.options[this.selectedIndex] //this refers to "selectmenu"
    if (chosenoption.value!="nothing"){
     var s = chosenoption.text;
     openFile("C:\PerlTest\test.txt");
    } 
   }
  }

EX. not working trying to pass parameter:

function openFile(name){
 XMLHttp.open("GET", "/cgi-bin/readFile.pl"+name, true);
 XMLHttp.onreadystatechange = function() {
 if (XMLHttp.readyState == 4) {
    document.getElementById("TxtArea").innerHTML = XMLHttp.responseText;
     }
     }
     XMLHttp.send(null);
   }

Im attempting to pass the paremeter in that way because of this example:

http://www.suite101.com/content/how-to-create-a-simple-perl-ajax-application-a136201

Can anyone help??

Thanks a lot.


After the sugestion of Kinopiko, that makes sense, i have the following:

HTML-

function openFile(name){
            XMLHttp.open("GET", "/cgi-bin/readFile.pl?file="+encodeURI(name), true);
            XMLHttp.onreadystatechange = function() {
            if (XMLHttp.readyState == 4) {
                var container = XMLHttp.responseText.split("\n");
                if (container.length>0){
                    for ( i=0; i< container.length-1;i++){
                        document.getElementById("TxtArea").innerHTML += container[i] + " ";
                    }
                }
            }
            else{
                document.getElementById("TxtArea").innerHTML = "333";//XMLHttp.responseText;
            }
            }
            XMLHttp.send(null);
        }

Perl script:

#!c:/Perl/bin/perl

use strict;
use CGI qw/param/;  
use URI::Escape; 

print "Content-type: text/html\n\n";

my $file = param ('file'); 
$file = uri_unescape ($file); 

open (MYFILE, $file); 
    while (<MYFILE>) {
        chomp;
        print "$_\n";
}
close (MYFILE); 

Now i dont get error in javascript, but my XMLHttp.readyState is never 4, so i dont get the content of the file.

Maybe im using the encodeURI wrong??

Thanks.

2
  • Why don't you use jQuery? jquery.com I can help with some examples if needed. Commented Dec 20, 2010 at 10:39
  • or prototype.js. Or Dojo. ... There are loads of Javascriptslibraries, not just jQuery. They can all make it easier to write. Commented Dec 20, 2010 at 12:06

2 Answers 2

4

First of all you need to add a question mark:

XMLHttp.open("GET", "/cgi-bin/readFile.pl?file="+name, true);

Also you need to percent-encode "name" using encodeURI.

On the Perl end, you can use a module like CGI to get the value of the file:

 use CGI qw/param/;
 my $file = param ('file');

Then

 use URI::Escape;
 $file = uri_unescape ($file);
Sign up to request clarification or add additional context in comments.

3 Comments

Some how the parameter is still no passing. The perl script doens´t execute. If i put the path hardcoded in the perl script i get the result that i want.
I accepted it bc your answer corrected my code, the fact that i was using the slashes wrong, it was my fault.
1
open (MYFILE, $file); 

should be

open (MYFILE, $file) or die "Cannot open file $file: $!\n";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.