Link to home
Start Free TrialLog in
Avatar of kevbob650
kevbob650Flag for United States of America

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/json');
      print to_json($returnString);
      exit(0);
   }

   nothing being returned (duh), but can't use return statement outside of a sub?
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

You appear to be missing a '}' at the end of the javascript function.
function checkUser() {
    $.ajax({
            dataType: 'json',
            type: 'POST',
            data: {ajaxAction: "checkUser"},
            url: 'cgi-bin/ajaxFuncs.pl',
            success: function(data) {
            $("#testdiv").html(data);
        }
   });
}

Open in new window

Avatar of kevbob650

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
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
<!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>

Open in new window


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);
#	}

Open in new window

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.
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/json');
      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?
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?
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!
'775' does not set the 'execute' bit.  Here's a screenshot of my Perl directory with permissions on my Ubuntu system.
User generated image
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
It was the AddHandler, thanks for all your time and help!
You're welcome, glad to help.