From 9d37bced7dd4873f32f1374d2914e27bb984c422 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 13 Jan 2014 12:20:24 -0500 Subject: view: support jsonp callback --- cgi-bin/view | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'cgi-bin/view') diff --git a/cgi-bin/view b/cgi-bin/view index 526d268..3994117 100755 --- a/cgi-bin/view +++ b/cgi-bin/view @@ -53,6 +53,7 @@ sub main{ $params->{$key} = $value; } + my $callback = $params->{'callback'} || undef unless(defined $params->{f}){ not_specified_error("f"); @@ -90,11 +91,23 @@ sub main{ $res = mysql_retrieve('select shaders.* from shader_ids cross join shaders on shader_ids.id = shaders.shader_id'); } default { - print $json->pretty->encode ({ ERROR => "Function parameter f improperly defined"}); + if (defined $callback) { + print $callback + "("; + print $json->pretty->encode ({ ERROR => "Function parameter f improperly defined"}); + print ")"; + } + else { + print $json->pretty->encode ({ ERROR => "Function parameter f improperly defined"}); + } exit(1); }; } - print $res; + if (defined $callback) { + print $callback + "(" + $res + ")"; + } + else { + print $res; + } } main(); exit(0); -- cgit v1.2.3-70-g09d2 From 94e950d6c0d1930f8f30240cac9b10c56e26c4d9 Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 13 Jan 2014 12:30:03 -0500 Subject: single json call --- cgi-bin/view | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'cgi-bin/view') diff --git a/cgi-bin/view b/cgi-bin/view index 3994117..e4f4f2d 100755 --- a/cgi-bin/view +++ b/cgi-bin/view @@ -15,13 +15,25 @@ my $sql_passwd = "gTYgT&M6q"; sub not_specified_error{ -my $param = shift; - print $json->pretty->encode ({ ERROR => sprintf("Function parameter %s not defined", $param ) }); + my $param = shift; + my $callback = shift; + response({ ERROR => sprintf("Function parameter %s not defined", $param ) }, $callback); exit(1); } +sub response{ + my $res = $json->pretty->encode(shift); + my $callback = shift; + if (defined $callback){ + print $callback + "(" + $res + ")"; + } + else { + print $res; + } +} our $dbh = DBI->connect("DBI:mysql:$sql_dbname", $sql_username, $sql_passwd) or die "Couldn't connect to database: " . DBI->errstr; + sub mysql_retrieve{ my ($statement, @args) = @_; my $sth = $dbh->prepare($statement) @@ -37,11 +49,11 @@ sub mysql_retrieve{ push(@rows, $data); } if ($sth->rows == 0) { - return $json->pretty->encode ({ ERROR => "No ids matched `$data_id'.\n\n" }); + return { ERROR => "No ids matched `$data_id'.\n\n" }; } $sth->finish; - return $json->pretty->encode({ SUCCESS => 1, data => \@rows }); + return { SUCCESS => 1, data => \@rows }; } sub main{ @@ -56,7 +68,7 @@ sub main{ my $callback = $params->{'callback'} || undef unless(defined $params->{f}){ - not_specified_error("f"); + not_specified_error("f", $callback); } my $res; @@ -90,24 +102,12 @@ sub main{ when(/originals/){ $res = mysql_retrieve('select shaders.* from shader_ids cross join shaders on shader_ids.id = shaders.shader_id'); } - default { - if (defined $callback) { - print $callback + "("; - print $json->pretty->encode ({ ERROR => "Function parameter f improperly defined"}); - print ")"; - } - else { - print $json->pretty->encode ({ ERROR => "Function parameter f improperly defined"}); - } + default { + response({ ERROR => "Function parameter f improperly defined"}, $callback); exit(1); }; } - if (defined $callback) { - print $callback + "(" + $res + ")"; - } - else { - print $res; - } + response($res, $callback); } main(); exit(0); -- cgit v1.2.3-70-g09d2 From 9401ffc256bc39dc085ebea21de4942083219b88 Mon Sep 17 00:00:00 2001 From: Jules Date: Mon, 13 Jan 2014 12:44:51 -0500 Subject: semicolon --- cgi-bin/view | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cgi-bin/view') diff --git a/cgi-bin/view b/cgi-bin/view index e4f4f2d..2e9a540 100755 --- a/cgi-bin/view +++ b/cgi-bin/view @@ -65,7 +65,7 @@ sub main{ $params->{$key} = $value; } - my $callback = $params->{'callback'} || undef + my $callback = $params->{'callback'} || undef; unless(defined $params->{f}){ not_specified_error("f", $callback); -- cgit v1.2.3-70-g09d2