in reply to Read JSON data from a form

You try to read a parameter named dataString but your Javascript never sends it.

Maybe you can show us what parameters are actually received by the Perl code? Also, what error message are you receiving on the Perl side of things from the JSON module?

Also, even if it works, currently your Perl script won't necessarily return a well-formatted JSON string to Javascript. Use encode_json instead of Data::Dumper for that.

Replies are listed 'Best First'.
Re^2: Read JSON data from a form
by Anonymous Monk on Dec 29, 2013 at 19:18 UTC
    OK, the form sends stuff like this:
    my $name = $q->param( 'name' ) || ''; my $email = $q->param( 'email' ) || ''; my $zip = $q->param( 'zip' ) || '';
    But by serializing the data this code
    var formData = $("#reply-form").serialize(); alert(formData);
    adds all the values into a string. Doesnt this line: my $data = decode_json($q->param('dataString')); grabs all that the from is sending?

      No. What part of CGI mentions the magic name dataString, or what other documentation makes you assume that?

      If you send your elements via jQuery.serialize(), you will get the elements as separate parameters in the request and can retrieve them as you already showed in your first Perl snippet.

      Your second Perl snippet makes no sense in this context.

        OK, I still need to use "$q->param(" for each value I am sending from the form, so whats the point in using JSON? Can you show any example based on this question? Thanks!