Skip to content

Commit

Permalink
JSONP support -- taken from Catalyst::Action::Serialize::JSONP
Browse files Browse the repository at this point in the history
  • Loading branch information
bricas committed Nov 14, 2011
1 parent f7dfada commit a6001c2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/Catalyst/Action/Serialize/SixteenColors/API/JSON.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,28 @@ before 'execute' => sub {
}
};

after 'execute' => sub {
my $self = shift;
my ($controller, $c) = @_;

# Ripped from Catalyst::Action::Serialize::JSONP

my $callback_key = (
$controller->{ serialize } ?
$controller->{ serialize }->{ callback_key } :
$controller->{ callback_key }
) || 'callback';

my $callback_value = $c->req->param( $callback_key );
if ( $callback_value ) {
if ($callback_value =~ /^\w+$/ ) {
$c->res->content_type( 'text/javascript' );
$c->res->output( $callback_value . '(' . $c->res->output() . ');' );
}
else {
warn 'Callback: '.$callback_value.' will not generate valid Javascript. Falling back to JSON output';
}
}
};

1;

0 comments on commit a6001c2

Please sign in to comment.