using a template to create a JSON structure to send back to an ajax request
As the subject line says, I would like to use a template to create a JSON structure that I would like to send back to the browser in response to an ajax request. Is that possible? Thanks. -- Puneet Kishor Sent with Sparrow
On 18/01/11 1:05 PM, Puneet Kishor wrote:
As the subject line says, I would like to use a template to create a JSON structure that I would like to send back to the browser in response to an ajax request. Is that possible?
Hi Puneet, I believe so, the following code snippet works for me. sub json { return 'application/json'; } get '/testjson' => sub { content_type json; return template 'json.tt', { r => { id => 1, name => "Widgets Co."} }, { layout => undef }; }; Where json.tt is { "name": "<% r.name %>", "id":<% r.id %> } Cheers Matt
Matthew Vickers wrote:
On 18/01/11 1:05 PM, Puneet Kishor wrote:
As the subject line says, I would like to use a template to create a JSON structure that I would like to send back to the browser in response to an ajax request. Is that possible?
Hi Puneet,
I believe so, the following code snippet works for me.
sub json { return 'application/json'; }
get '/testjson' => sub { content_type json; return template 'json.tt', { r => { id => 1, name => "Widgets Co."} }, { layout => undef }; };
Where json.tt is
{ "name": "<% r.name %>", "id":<% r.id %> }
Oh! Brilliant! That is exactly what I was looking for. I am going to try it out and report back. Many thanks, Puneet.
I have added an "issue" on github for this request https://github.com/sukria/Dancer/issues#issue/258 Thanks, Puneet Kishor wrote:
As the subject line says, I would like to use a template to create a JSON structure that I would like to send back to the browser in response to an ajax request. Is that possible?
Thanks.
-- Puneet Kishor Sent with Sparrow <http://www.sparrowmailapp.com>
-- Puneet Kishor
participants (2)
-
Matthew Vickers -
Puneet Kishor