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

Ok, take a step back, try this simple script
#!/usr/bin/env perl use strict; use warnings; use CGI; use CGI::Carp('fatalsToBrowser'); use JSON; my $q = CGI->new; print $q->header(), $q->start_html(-title=>"Hello World"), $q->h2("Hello World"), $q->end_html();
I noticed on first line of your cgi that worked you had #!/usr/bin/perl. If this script doesn't work try changing that.
poj

Replies are listed 'Best First'.
Re^10: json return value
by stenasc (Novice) on Jul 16, 2013 at 12:40 UTC

    Same thing when accessed through browser

    Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, webmaster@tyder.com and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

    Server log is...

    16/Jul/2013:13:14:25 +0100 "GET /cgi-bin/test2.cgi HTTP/1.1" 404 - "-" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"

        This novice is back again. My cgi works when the following line of code is used as it uses the parameters after the logical or operator...

        my $data = $q->param('POSTDATA') || '{"a":100,"b":300}';

        but it doesn't when the following line is used...

        my $data = $q->param('POSTDATA'};

        I'm posting up the following json statement which I checked on jsonlint and gave no errors.

        {"par":12345678912345,"id":20}

        I get the following webserver error message

        File does not exist: /home/myurl/public_html/500.shtml

        It looks to as if the parameters are not parsed correctly or are not being received properly. Any help greatly appreciated

Re^10: json return value
by stenasc (Novice) on Jul 16, 2013 at 14:01 UTC

    That worked, but need a slight change...

    #!/usr/bin/env perl use strict; use warnings; use CGI; use CGI::Carp('fatalsToBrowser'); use JSON::PP(); my $q = CGI->new; print $q->header(), $q->start_html(-title=>"Hello World"), $q->h2("Hello World"), $q->end_html();
      Ok so try again with JSON::PP
      #!/usr/bin/env perl use strict; use warnings; use CGI (); use JSON::PP (); my $q = CGI->new; my $data = $q->param('POSTDATA') || '{"a":100,"b":300}'; my $json = JSON::PP->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 });
      poj

        It is finally working. Thank you to all the monks that replied and showed a great deal of patience. Couple of things causing the problems...

        The server was not set up correctly. Took a while for the administrator to accept this, but when he did, things started to work. It's frustrating when you are running round in circles, trying to tell these guys why it should be working and they're telling you something is wrong with your code, only to find out later that it was a server side issue.

        JSON was not allowed installed on the server as it was shared...dunno why. Had to use JSON::PP.

        I really do appreciate, the help and especially the code examples. This is an excellent forum and my faith has well and truly been restored.

        Fantastic. Thank you...now we're getting somewhere. When I loaded up the cgi in the browser, I get {"c":400}. Now just need to get the perl client working.