Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Post from jQuery to Perl - can't access parameters

by golux (Chaplain)
on Mar 13, 2013 at 14:24 UTC ( [id://1023211]=note: print w/replies, xml ) Need Help??


in reply to Post from jQuery to Perl - can't access parameters

Hi stuckdev,

Here's a fairly simple example that should do the basics of what you want:

#!/usr/bin/perl -w ############### ## Libraries ## ############### use strict; use warnings; use CGI; use CGI::Carp qw{ fatalsToBrowser }; use JSON; ################## ## User-defined ## ################## my $jquery = "//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min. +js"; ################## ## Main program ## ################## my $query = new CGI; my $url = $query->url; my $txt_content = $query->param("txt_content") || ""; if ($txt_content) { handle_server_side_ajax($txt_content); exit; } print_html(); ################# ## Subroutines ## ################# sub handle_server_side_ajax { my ($text) = @_; print "Content-type: application/json\n\n"; # Do something with $text ... my $result = "You said '$text'"; my $time = localtime(time()); my $h_json = { 'result' => $result, 'time' => $time }; my $output = to_json($h_json); print $output; } sub print_html { print "Content-type: text/html\n\n"; print qq{ <script src="$jquery" type="text/javascript"></script> <script type="text/javascript"> var J = jQuery.noConflict(); function ajax_text() { var input = J('#text_input').val(); J.ajax({ url: "$url", cache: false, dataType: 'json', data: { txt_content: input }, success: function(json) { handle_ajax_output(json); } }); } function handle_ajax_output(json) { var result = json.result; var time = json.time; var text = "<br>Time: " + time + " &nbsp; Result: " + +result; J('#html_result').append(text); } </script> <body style="background:cyan"> <h2>JQuery Example</h2> <br>Enter Text and click Submit <br> <input id="text_input"> <input type="button" value="Submit" onclick="ajax_text() +"> <br><br> <h5>Ajax Results Below</h5> <pre id="html_result" style="background:#ffef9f"> </pre> </body> }; }

Note that I used "var J = jQuery.noConflict();" so that I could type:

var input = J('#text_input').val();

without having to escape the usual '$' variable of jQuery. Otherwise I'd have had to do:

var input = \$('#text_input').val();

My guess is that you were getting a blank page because you didn't print the headers properly. For the server-side ajax, you need to at least print something like:

print "Content-type: text/html\n\n";

Or, what I usually use:

print "Content-type: application/json\n\n";

If you have access to your httpd logs (for example, in Linux they might be in "/var/log/httpd"), take a look at "access_log" (to see if the server-side code was called), and "error_log" (to see whether there were problems).

By the way, you don't have to use jQuery to do what you're trying to do, but it does make it a lot easier (especially things like Ajax, which are greatly simplified by jQuery).

Let me know if you have any questions about my code, and I'll be happy to elaborate further.

say  substr+lc crypt(qw $i3 SI$),4,5

Replies are listed 'Best First'.
Re^2: Post from jQuery to Perl - can't access parameters
by stuckdev (Initiate) on Mar 13, 2013 at 15:28 UTC

    Thanks. I've tried putting in your code. I have two issues (thus far). I'm getting the error:

    Can't locate JSON.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at hello_world.pl line 8. BEGIN failed--compilation aborted at hello_world.pl line 8.

    on the server. (I'm hosting from 2mhost, so I have no idea what this all means) Do they even have JSON?

    The other issue, is that my jquery post is directly in my HTML page, not within a function on a PERL page -- so I'm having a hard time extrapolating your code and putting it into straight HTML format because I'm not exactly what you're doing with the whole J. stuff.

      Hi stuckdev,

      As marto said, you need to have JSON installed. You could ask 2mhost to install it for you, or try to install it locally (if you have that capability). It's not a complete requirement, but JSON ("Javascript Object Notation") is a wonderful means of simplifying how data is serialized for passing between client and server.

      As for your HTML page, if it's separate from your CGI, you can just put everything printed out by subroutine "print_html() into the html page, except for the "Content-type:  text/html" (which will be printed for you automatically by the server, since it's in HTML.

      You would then change $url in the function ajax_text(), in this line:

      url: "$url",

      to be whatever the name of your actual CGI/Perl script was instead.

      However, please note that you will still need the headers printed from your server-side Perl/CGI script; eg.:

      print "Content-type: application/json\n\n";
      If you forget that, you'll get something like this error (from my /var/log/httpd/error_log file):
      [Wed Mar 13 11:41:37 2013] [error] [client 192.168.17.33] Premature en +d of script headers: 1023196.cgi, referer: http://mymachine.com/10231 +96.cgi

      Does that help you get further?

      say  substr+lc crypt(qw $i3 SI$),4,5

      This means your host don't have JSON instaled, depending on your hosting deal you may be able to install modules easily, or it could be something they have to do for you. This is not a Core module. Also 5.8.8 is a fairly old verison of Perl. See also here and here.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1023211]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found