0

I am trying to POST JSON using JavaScript and read the POST results using a Perl script. I have written this code but am unable to get the Perl script to read in the JSON text.

HTML:

<!DOCTYPE html>
<html>
<head>
<title>Testing ajax</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var d = {
    "name": "Bob",
    "sex": "Male",
    "address": {
        "city": "San Jose",
        "state": "California"
    },
    "friends": [
        {
            "name": "Alice",
            "age": "20"
        },
        {
            "name": "Laura",
            "age": "23"
        },
        {
            "name": "Daniel",
            "age": "30"
        }
    ]
};

$(document).ready(function() {
    $("#test").click(function() {
        $.ajax({
            type: 'POST',
            url: '/cgi-bin/Raghav_test/Apollo/read_ajax3.pl',
            data: "r=" + d,
            success: function(res) { alert("data" + res); },
            error: function() { alert("did not work"); }
        });
    });
});
</script>
</head>
<body>
<button id="test">Testing</button>
</body>
</html>

Perl:

#!/usr/bin/perl -w

use CGI;
#use DBD;
use DBI;
use JSON::PP;
use Data::Dumper;
use DBD::Oracle qw(:ora_types);
use lib "/var/www/cgi-bin/ICE_LIBRARY/";

require '/var/www/cgi-bin/import_scripts/library/common_lib.pl';
require "/var/www/cgi-bin/import_scripts/library/script_log.pl";

use database_conf;
my $db = new database_conf;

#my $EP_dev_conn = $db->db_eportal_dev;
my $EP_prod_conn = $db->db_eportal_prod;
my $cgi = CGI->new;
my $id = $cgi->param("r");
#my $data = $cgi->param('POSTDATA');

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

#my $value =  $ddata->{'address'}{'city'} ;
# Here I'd like to receive data from jQuery via ajax.
#my $id = $cgi->param('apiKey');
#$json = qq{{"ID" : "$id"}};
#my $method = $cgi->param('method');
#my $ip = $cgi->param('ip');

$json = qq{"$id"};
print $json;
exit;
5
  • 1
    You need to call JSON.stringify() on the object you put into the request, and then you need to decode the JSON string in your Perl code. Commented Nov 5, 2015 at 21:27
  • 2
    Also, you should edit your post to include the output you are seeing, rather than just saying that, essentially, "it's not working". Commented Nov 5, 2015 at 21:29
  • Did my answer solve your problem? Commented Nov 10, 2015 at 18:09
  • Thanks for the help, Matt. Yes this does work. Commented Mar 9, 2016 at 18:53
  • Great! Please see: What should I do when someone answers my question? Commented Mar 9, 2016 at 21:09

1 Answer 1

1

You need to call JSON.stringify() on your object before making the request:

$.ajax({
    type: 'POST',
    url: '/cgi-bin/Raghav_test/Apollo/read_ajax3.pl',
    data: { "r": JSON.stringify(d) },
    success: function(res) { alert("data" + res); },
    error: function() { alert("did not work"); }
});

And then you need to call decode_json() on the string to parse it and turn it into a Perl data structure:

my $q    = CGI->new;
my $json = $q->param("r");
my $href = decode_json($json);

print Dumper($href);

Output:

$VAR1 = {
          'address' => {
                         'state' => 'California',
                         'city' => 'San Jose'
                       },
          'name' => 'Bob',
          'friends' => [
                         {
                           'name' => 'Alice',
                           'age' => '20'
                         },
                         {
                           'age' => '23',
                           'name' => 'Laura'
                         },
                         {
                           'name' => 'Daniel',
                           'age' => '30'
                         }
                       ],
          'sex' => 'Male'
        };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.