This is a typical error, see Server error, see CGI Help Guide , Troubleshooting Perl CGI scripts, CGI::Carp, DebugCGI , CGI::Carp::Fatals

The checklists work, but here you go , first try

#!/usr/bin/perl -- use strict; use warnings; use CGI (); use CGI::Carp qw(fatalsToBrowser); use JSON (); my $q = CGI->new; my $data = $q->param('POSTDATA') ; #~ my $data = $q->param('POSTDATA') || '{"a":100,"b":300}'; my $json = JSON->new->utf8; my $input = $json->decode( $data ); my $a = $input->{a}; my $b = $input->{b}; my $c = $a + $b; print $q->header("application/json"); print $json->encode({ c => $c });

then try

#!/usr/bin/perl -- use strict; use warnings; use CGI (); use CGI::Carp qw(fatalsToBrowser); use JSON (); Main( @ARGV ); exit( 0 ); sub Main { binmode STDOUT; my $q = CGI->new; my $cb = AddJsonGetBytes( $q ); print $q->header("application/json"), $cb; return; } sub AddJsonGetBytes { my( $q ) = @_; my $data = $q->param('POSTDATA') ; my $json = JSON->new->utf8; my $input = $json->decode( $data ); my $a = $input->{a}; my $b = $input->{b}; my $c = $a + $b; return $json->encode({ c => $c }); }

then try

#!/usr/bin/perl -- use strict; use warnings; use CGI (); use CGI::Carp qw(fatalsToBrowser); use JSON (); Main( @ARGV ); exit( 0 ); sub Main { binmode STDOUT; my $q = CGI->new; my $cb = AddJsonGetBytes( $q ); print $q->header("application/json"), $cb; return; } sub AddJsonGetBytes { my( $q ) = @_; my $json = JSON->new->utf8; my $data = $q->param('POSTDATA') ; if( not defined $data ){ return $json->encode({ error => 'no valid input' }); } my $input = $json->decode( $data ); my $a = $input->{a}; my $b = $input->{b}; my $c = $a + $b; return $json->encode({ c => $c }); }

then try

#!/usr/bin/perl -- use strict; use warnings; use CGI (); use CGI::Carp qw(fatalsToBrowser); use JSON (); Main( @ARGV ); exit( 0 ); sub Main { binmode STDOUT; my $q = CGI->new; my $json = JSON->new->utf8; my $cb = AddJsonGetBytes( $q , $json ); print $q->header("application/json"), $cb; return; } sub AddJsonGetBytes { my( $q, $json ) = @_; my $data = $q->param('POSTDATA') ; if( not defined $data ){ return $json->encode({ error => 'no valid input' }); } my $input = $json->decode( $data ); my $a = $input->{a}; my $b = $input->{b}; my $c = $a + $b; return $json->encode({ c => $c }); }

:)


In reply to Re^7: json return value by Anonymous Monk
in thread json return value by stenasc

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.