Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^7: json return value

by Anonymous Monk
on Jul 16, 2013 at 08:23 UTC ( #1044533=note: print w/replies, xml ) Need Help??


in reply to Re^6: json return value
in thread json return value

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 }); }

:)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1044533]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2022-05-28 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (98 votes). Check out past polls.

    Notices?