Featured post
javascript - Call Perl function in HTML using AJAX -
im having setback, im making html page javascript, , im using ajax call perl functions. thing when perl program doesn't need parameters calling trivial. have function open , read file need give location of file perl script, having passe trough paramenter in ajax calling. ex working call:
function getoption(){ var selectmenu=document.getelementbyid("select1") selectmenu.onchange=function(){ //run code when "onchange" event fires var chosenoption=this.options[this.selectedindex] //this refers "selectmenu" if (chosenoption.value!="nothing"){ var s = chosenoption.text; openfile("c:\perltest\test.txt"); } } }
ex. not working trying 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 pass paremeter in way because of example:
http://www.suite101.com/content/how-to-create-a-simple-perl-ajax-application-a136201
can help??
thanks lot.
after sugestion of kinopiko, makes sense, have 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){ ( 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"; $file = param ('file'); $file = uri_unescape ($file); open (myfile, $file); while (<myfile>) { chomp; print "$_\n"; } close (myfile);
now dont error in javascript, xmlhttp.readystate never 4, dont content of file.
maybe im using encodeuri wrong??
thanks.
first of need add question mark:
xmlhttp.open("get", "/cgi-bin/readfile.pl?file="+name, true);
also need percent-encode "name" using encodeuri
.
on perl end, can use module cgi value of file:
use cgi qw/param/; $file = param ('file');
then
use uri::escape; $file = uri_unescape ($file);
- Get link
- X
- Other Apps
Comments
Post a Comment