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?
kevbob650Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
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

0
kevbob650Author Commented:
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
0
Dave BaldwinFixer of ProblemsCommented:
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

0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

kevbob650Author Commented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
You may also need to tell your web server to 'execute' it and not just return the text.
0
kevbob650Author Commented:
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");
        }
    });
}
0
Dave BaldwinFixer of ProblemsCommented:
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?
0
kevbob650Author Commented:
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.
0
Dave BaldwinFixer of ProblemsCommented:
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?
0
kevbob650Author Commented:
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!
0
Dave BaldwinFixer of ProblemsCommented:
'775' does not set the 'execute' bit.  Here's a screenshot of my Perl directory with permissions on my Ubuntu system.
Perl directory showing permissions
0
Dave BaldwinFixer of ProblemsCommented:
And this is the Add-Handler line in 'apache2.conf':
AddHandler cgi-script .cgi .pl .py

Open in new window

'py' is for Python.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kevbob650Author Commented:
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" ?
0
Dave BaldwinFixer of ProblemsCommented:
No hurry.  See what happens tomorrow.
0
kevbob650Author Commented:
It was the AddHandler, thanks for all your time and help!
0
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.