Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5845855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:29:41+00:00 2026-05-22T12:29:41+00:00

I am making a webpage, where there are two forms (users and owners), where

  • 0

I am making a webpage, where there are two forms (users and owners), where one can type comma separated names in each form, and hit Submit.

I have tried to use JSON before with strings only, without using a Perl module. Now I need to transfer information for many users in the form of username, full name and owner_name, full name. In Perl I have this as two hashes.

This is a two part question.

Server side

This is my Perl script.

#!/usr/bin/perl -T

use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(:standard);
use JSON;
use utf8;
use strict;
use warnings;

my $cgi = CGI->new;
$cgi->charset('UTF-8');

my @owners = map { s/\s*//g; $_ } split ",", $cgi->param('owner');
my @users  = map { s/\s*//g; $_ } split ",", $cgi->param('users');

# ...

my %user_result = ();
foreach my $u (@users) {
    $user_result{$u} = $db1->{$u}{displayName};
}

my %owner_result = ();
foreach my $o (@owners) {
    $owner_result{$o} = $db2{$o}{ad_displayname};
}

my $json->{"users"}  = \%user_result;
my $json->{"owners"} = \%owner_result;

my $json_string = to_json($json);

print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json_string;

Is this the correct way to make the two hashes into a JSON string?

Client side

$(document).ready(function(){
    $('form').live('submit', function(){
    $.ajax({
        type: "GET",
        url: "/cgi-bin/ajax.pl",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        data: $(this).serialize(),

        error: function(XMLHttpRequest, textStatus, errorThrown) { 
        $('div#create_result').text("responseText: " + XMLHttpRequest.responseText +
                        ", textStatus: " + textStatus +
                        ", errorThrown: " + errorThrown);
        $('div#create_result').addClass("error");
        },

        success: function(result){
        if (result.error) {
            $('div#create_result').text("result.error: " + result.error);
            $('div#create_result').addClass("error");
        } else { // perl script says everything is okay

              // how do I access my two hashes here?
              // {"users": [username1, fullname1, ...], "owners": [owner name1, full name 1, ...]}

              var users  = result.users;    // array
              var owners = result.owners;   // array
              alert(users);
              alert(owners);

        }
        }
    });
    });
});

Is this the correct way to send and receive the JSON?

And how do I access data afterwards?

Update
Updated code with Matt Ball and davorg answers and added alert(users).

The bug right now is that the json object isn’t defined.

my $json->{"users"}  = \%user_result;
my $json->{"owners"} = \%owner_result;

How do I define such?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:29:42+00:00Added an answer on May 22, 2026 at 12:29 pm

    You’re attempting to print two CGI header blocks. Unfortunately, you can only have a single CGI block associated with each response. And, unfortunately, the first header block you print has the wrong content-type header (it’s ‘text/html’ where you actually want ‘application/json’).

    If you remove the ‘print “Content-Type: …”‘ line, things should work a bit better.

    Then in the second header block, you’re trying to call an unknown function called “header”. That’s actually a method on your $cgi object, so it needs to be called as $cgi->header(…).

    Those fixes should actually get some JSON back to your client code successfully. There are a couple of other suggestions I’d make though.

    1/ The first argument to the ‘split’ function should be a regular expression, not a string.

    2/ The JSON data structures you are building seem unnecessarily complex. For each of your two sections (users and owners) you have a single element array which contains a reference to a hash. Surely the array can be omitted so each element just contains the hash reference directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a webpage with dynamic content that enters the view with AJAX polling.
I'm working on a web page where I'm making an AJAX call that returns
Making echo of a question around the web: Is the syntax for svn:ignore patterns
When making changes using SubmitChanges() , LINQ sometimes dies with a ChangeConflictException exception with
After making some changes in my models (eg. new field in a model and
After making a few modifications to a rails app I am tinkering on, railroad
Im making a small python script to upload files on the net. The script
I've been making a concerted effort to improve my javascript skills lately by reading
I am making a simple game in order to learn a new language. I
I'm thinking about making a networked game. I'm a little new to this, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.