最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call Perl function in HTML using AJAX - Stack Overflow

programmeradmin3浏览0评论

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:

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.

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./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.

Share edited Dec 20, 2010 at 12:04 Catarrunas asked Dec 20, 2010 at 10:37 CatarrunasCatarrunas 433 silver badges8 bronze badges 2
  • Why don't you use jQuery? jquery. I can help with some examples if needed. – Fred 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. – Colin Fine Commented Dec 20, 2010 at 12:06
Add a ment  | 

2 Answers 2

Reset to default 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);
open (MYFILE, $file); 

should be

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

评论列表(0)

  1. 暂无评论