Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Read JSON data from a form

by tangent (Parson)
on Dec 29, 2013 at 20:06 UTC ( [id://1068700]=note: print w/replies, xml ) Need Help??


in reply to Read JSON data from a form

You seem to be confusing the send and return aspects. You are not sending JSON to the script as the serialize() method serializes a form's data into a query string, not JSON (this string can be decoded using the normal CGI methods). On the other hand, you are telling jQuery that your response will be JSON ( dataType: "json" in your javascript and $q->header("application/json") in your perl script) but you are not sending JSON back, just a plain string.

I don't know your exact requirements but I suspect you don't need to send JSON to your script, you may however need to return JSON. This may help you to see the difference:

Javascript

<script type="text/javascript" > $(function() { $(".submit").click(function() { var formData = $("#reply-form").serialize(); alert(formData); $.ajax({ type: "post", url: "code.pl", cache: false, data: formData, dataType: "json", success: function(data) //onSuccess, { alert(data.calling_params); }, error: function() { alert( "Sorry, there was a problem!" ); } }); return false; }); }); </script>

Perl

use strict; use warnings; use CGI; use JSON; use Data::Dumper; my $q = new CGI; my $in = $q->Vars; my $string = Dumper ($in); my $response = encode_json( { calling_params => "$string" } ); print $q->header("application/json"); print $response;

Replies are listed 'Best First'.
Re^2: Read JSON data from a form
by Anonymous Monk on Dec 29, 2013 at 21:42 UTC
    use CGI->param not CGI->Vars , CGI->Vars has caveats
      You are right, but I didn't know the names of the parameters at the time of writing. I can now see those from OP's reply so you could change the offending line to:
      my $in; $in->{'name'} = $q->param('name'); $in->{'email'} = $q->param('email'); # etc
        FWIW, you don't need to know the name, param without args provides the names , Vars mangles (encodes, serializes, packs, implodes) the data, its backwards compatibility for some 1993 stuff
        #!/usr/bin/perl -- use Data::Dump qw/ dd /; use CGI; my $q = CGI->new(q{a=b;a=c;d=e;f=g}); my %Vars = map { $_ => [ $q->param($_) ] } $q->param(); dd( $q ); dd( $q->Vars ); dd( \%Vars ); __END__ __END__ bless({ ".charset" => "ISO-8859-1", ".fieldnames" => {}, ".parameters" => ["a", "d", "f"], "escape" => 1, "param" => { a => ["b", "c"], d => ["e"], f => ["g"] }, "use_tempfile" => 1, }, "CGI") ("a", "b\0c", "d" .. "g") { a => ["b", "c"], d => ["e"], f => ["g"] } bless({ ".charset" => "ISO-8859-1", ".fieldnames" => {}, ".parameters" => ["a", "d", "f"], "escape" => 1, "param" => { a => ["b", "c"], d => ["e"], f => ["g"] }, "use_tempfile" => 1, }, "CGI")
        #!/usr/bin/perl -- use Data::Dump qw/ dd /; use CGI; my $q = CGI->new(q{a=b;a=c%00d;d=e;f=g}); my %Vars = map { $_ => [ $q->param($_) ] } $q->param(); dd( $q ); dd( $q->Vars ); dd( \%Vars ); __END__ bless({ ".charset" => "ISO-8859-1", ".fieldnames" => {}, ".parameters" => ["a", "d", "f"], "escape" => 1, "param" => { a => ["b", "c\0d"], d => ["e"], f => ["g"] }, "use_tempfile" => 1, }, "CGI") ("a", "b\0c\0d", "d" .. "g") { a => ["b", "c\0d"], d => ["e"], f => ["g"] }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-23 23:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found