kevbob650
asked on
How to have Perl return value to an Ajax call
I'm confused on how a value is returned to an Ajax call to Perl where a Perl function is not used, example:
function checkUser() {
$.ajax({
dataType: 'json',
type: 'POST',
data: {ajaxAction: "checkUser"},
url: 'cgi-bin/ajaxFuncs.pl',
success: function(data) {
$("#testdiv").html(data);
}
});
perl code... not inside a sub, following others code structure
if ( $q->param('ajaxAction') eq 'checkUser' ) {
my $returnString = 'this is the string';
print $q->header('application/js on');
print to_json($returnString);
exit(0);
}
nothing being returned (duh), but can't use return statement outside of a sub?
function checkUser() {
$.ajax({
dataType: 'json',
type: 'POST',
data: {ajaxAction: "checkUser"},
url: 'cgi-bin/ajaxFuncs.pl',
success: function(data) {
$("#testdiv").html(data);
}
});
perl code... not inside a sub, following others code structure
if ( $q->param('ajaxAction') eq 'checkUser' ) {
my $returnString = 'this is the string';
print $q->header('application/js
print to_json($returnString);
exit(0);
}
nothing being returned (duh), but can't use return statement outside of a sub?
ASKER
Sorry, that was just a typo in my submission. I'm not getting any js errors, only nothing is returned. If I add:
,
error: function() {
console.log('error');
}
'error' is printed to the console, so I'm doing something wrong on the perl side
,
error: function() {
console.log('error');
}
'error' is printed to the console, so I'm doing something wrong on the perl side
Below are my test pages that work. You may find that if you don't pass valid JSON formatted data back that you won't see anything. Which is why I used plain text for a test.
Ajax-Perl-Test.html
cgi-bin/ajaxFuncs.pl
Ajax-Perl-Test.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>AJAX Perl test</title>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
<!--
function checkUser() {
$.ajax({
// dataType: 'json',
type: 'GET',
data: {ajaxAction: "checkUser"},
url: 'cgi-bin/ajaxFuncs.pl',
// url: 'ip.php',
success: function(result) {
$("#testdiv").html(result);
}
});
}
// -->
</script>
</head>
<body onload="checkUser()">
<h1>AJAX Perl test</h1>
<div id="testdiv">*testdiv*</div>
<pre>
perl code... not inside a sub, following others code structure
if ( $q->param('ajaxAction') eq 'checkUser' ) {
my $returnString = 'this is the string';
print $q->header('application/json');
print to_json($returnString);
exit(0);
}
</pre>
</body>
</html>
cgi-bin/ajaxFuncs.pl
#if ( $q->param('ajaxAction') eq 'checkUser' ) {
my $returnString = 'This is the string 2';
#print $q->header('application/json');
print "Content-type: text/html\n\n";
# print to_json($returnString);
print $returnString;
exit(0);
# }
ASKER
Well now instead of returning "error" it returns the entire perl page in text? The actual perl page is quite large but if I run it in the console it runs just fine without any errors.
Then the Perl page is not 'executing'. You need to set permissions for it to 'run' as a program. Then it will return just the text.
You may also need to tell your web server to 'execute' it and not just return the text.
ASKER
still printing the code out, perhaps if I show you all of my code the error will reveal itself! I did strip the perl file down to basics. thanks for all of your trouble!
< perl file testfile.pl >
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use JSON;
use CGI;
my $q = CGI::new();
if ( $q->param('ajaxAction') eq 'checkUser' ){
my $returnString = 'this is the string';
print $q->header('application/js on');
print $returnString;
exit(0);
}
< ajax call from my index.htm file >
function checkUser() {
$.ajax({
//dataType: 'json',
type: 'POST',
data: {ajaxAction: "checkUser"},
url: 'cgi-bin/perltest.pl',
success: function(data) {
$("#testdiv").html(data);
},
error: function() {
console.log("error");
}
});
}
< perl file testfile.pl >
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use JSON;
use CGI;
my $q = CGI::new();
if ( $q->param('ajaxAction') eq 'checkUser' ){
my $returnString = 'this is the string';
print $q->header('application/js
print $returnString;
exit(0);
}
< ajax call from my index.htm file >
function checkUser() {
$.ajax({
//dataType: 'json',
type: 'POST',
data: {ajaxAction: "checkUser"},
url: 'cgi-bin/perltest.pl',
success: function(data) {
$("#testdiv").html(data);
},
error: function() {
console.log("error");
}
});
}
If it is "still printing the code out", then it is not a Perl error but a setup problem on the web server. Which web server are you using? Is it shared hosting?
ASKER
Well I'm trying this code with both apache and with node.js (two different files) and getting the same results. This is dedicated linux webserver.
Apache needs to have a 'handler' line for Perl with 'pl' and 'cgi' listed. I don't know about node.js. Is there an 'exec' function in node.js? And are your permissions on your Perl files set to 'execute' so they run without invoking the Perl interpreter first?
ASKER
I set all of the perl files with a permission level of 775, so that shouldn't be an issue. I am able to run a perl perltest.pl from the command line and I don't see any error but I guess that has little to do with it not working when called from a browser? Sorry I am such a perl dunce. This is an enterprise server used by many and lots of perl running. I think I'll have to pester one of the other users and get some guidance. I'm missing something here!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well I'm still in limbo. I appreciate all of your help. I'll have to ping my coworkers who have apps running on this server. Not sure what I should do here, "accept as a solution" or "cancel" ?
No hurry. See what happens tomorrow.
ASKER
It was the AddHandler, thanks for all your time and help!
You're welcome, glad to help.
Open in new window