I have a page that sends JSON data with ajax to a perl file, but I'm a bit lost in retrieving the data. Many posts out there are about generating JSON and sending it, but it's not what I need. So here's the jquery section of the sending page:
$(function(){ // $Date is perl of course var json=jQuery.parseJSON(\'{"DATE":"'.$Date.'"}\'); json["address"]="123 some street"; // other functions generate more fields. and call postM() postM(); function postM(){ $.ajax({ url: "params.pl", type: "GET", processData: false, data: JSON.stringify(json), dataType: "text", async: true, success: function(msg){ alert("perl says: "+msg); } }); // ajax } // postM }); // jquery
and here's all of the param.pl file
#!/usr/bin/perl use strict; use CGI ':standard'; use CGI::Carp "fatalsToBrowser"; my $req=$ENV{QUERY_STRING}; my $i='0'; my $ref=$ENV{HTTP_REFERER}; my $p=param('data'); my $ad=param('address'); my $num_args = $#ARGV + 1; $i++ if $p; $i++ if $ad; $i++ if $req; my $file='json.txt'; open FH,'>',$file; print FH "R:$ref Q:$req A:$ad P:$p G:$ARGV[0] "; close FH; wait; my $fsz= -s $file; print header,"file Sz:$fsz ARGV: $num_args I: $i";
This works ok while jquery sections has type: "GET" but read POST is better way to handle things, though it fails if I change it. Also, when I do get the data, there's a bunch of backslashes and I feel there's got to be a better way. Also note: "processData" is set to false with jquery since I don't know the exact parameter names being transferred. Any help would be appreciated. Thank you perl monk masters.

In reply to Receiving JSON data with perl by JayBee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.