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?
 
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
 
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.