Dear dancer experts, I have a 1D Perl array which I would like to encode using JSON and send it as a response to an ajax request. I have implemented the following steps so far in my dancer app: *use Dancer::Plugin::Ajax;* *set serializer => 'JSON';* * * *ajax '/getarray' => sub {* * * * my @sendArray;* * ........... * * {* * @sendArray; # this is the array I intend to send to the client Java Script* * }* * * *};* The sendArray is not a (key, value) pair based structure, but a simple 1D array. Can anyone let me know if * "@sendArray"* statement at the end of ajax routine above will correctly encode to JSON? Also is there any way to print (to console) the final encoded JSON object from the ajax routine at the server end? thanks in advance \amk
{ @sendArray } won't do what you want because you're putting the array inside a hash reference. You either want to put it inside an array ref, or reference it, e.g. [ @sendArray ] or just \@sendArray. Of course you could also do: { results => \@sendArray } if you want your output to be a hash at the top level. Hope this helps! -- Rik Brown http://www.rikbrown.co.uk/ On Saturday, 6 July 2013 at 16:28, Anand Meher wrote:
Dear dancer experts, I have a 1D Perl array which I would like to encode using JSON and send it as a response to an ajax request.
I have implemented the following steps so far in my dancer app:
use Dancer::Plugin::Ajax; set serializer => 'JSON';
ajax '/getarray' => sub {
my @sendArray; ........... { @sendArray; # this is the array I intend to send to the client Java Script }
};
The sendArray is not a (key, value) pair based structure, but a simple 1D array. Can anyone let me know if "@sendArray" statement at the end of ajax routine above will correctly encode to JSON?
Also is there any way to print (to console) the final encoded JSON object from the ajax routine at the server end?
thanks in advance \amk
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm (mailto:dancer-users@dancer.pm) http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
On Sat, 6 Jul 2013 17:59:21 -0700 Rik Brown <rik@rikbrown.co.uk> wrote:
Of course you could also do:
{ results => \@sendArray }
if you want your output to be a hash at the top level.
Hope this helps!
Rik's answer above is the course of action I'd typically recommend - because even if you just want the array for now, in future you may well decide you want to provide other information from that call, too (perhaps be able to return an error message as a key named 'error', or return the number of results found as a key named 'count', or return the average of all the values... etc) - so returning a hashref as Rik illustrates helps with future-proofing, as you could easily add more information to the response without breaking existing code. -- David Precious ("bigpresh") <davidp@preshweb.co.uk> http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github
Thank you David and Rik for your answers, I have one more question, I tried executing some perl code from a ajax routine : something like : *sub get_stats {* *# some perl code goes here * * ## open a file using perl file open* * ## use dancer logger to output to console* * ## execute shell commands using system call of perl* *}* * * *ajax '/killprocess' => sub {* * get_stats;* * * *};* * * But it seems that the perl code is not getting executed, I do not have anything shown in my logger (console), am I doing something wrong over here ? Thank You \amk On Sun, Jul 7, 2013 at 12:38 PM, David Precious <davidp@preshweb.co.uk>wrote:
On Sat, 6 Jul 2013 17:59:21 -0700 Rik Brown <rik@rikbrown.co.uk> wrote:
Of course you could also do:
{ results => \@sendArray }
if you want your output to be a hash at the top level.
Hope this helps!
Rik's answer above is the course of action I'd typically recommend - because even if you just want the array for now, in future you may well decide you want to provide other information from that call, too (perhaps be able to return an error message as a key named 'error', or return the number of results found as a key named 'count', or return the average of all the values... etc) - so returning a hashref as Rik illustrates helps with future-proofing, as you could easily add more information to the response without breaking existing code.
-- David Precious ("bigpresh") <davidp@preshweb.co.uk> http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
The Ajax plugin only fires if your X-Requested-With header equals XMLHttpRequest - which will happen if you're using JS to do an Ajax request. If you just need to test this I'd suggest a browser extension like Postman which can be configured to send arbitrary HTTP headers. If you in fact just want a generic endpoint that works regardless of headers, just use a normal 'get' route and set 'serializer' in your config to be JSON. Away from my computer so can't dig up the docs, but it's all discussed in the Dancer manuals. Cheers Sent from my phone. On 7 Jul 2013 10:07, "Anand Meher" <kvmsanand@gmail.com> wrote:
Thank you David and Rik for your answers, I have one more question, I tried executing some perl code from a ajax routine : something like :
*sub get_stats {* *# some perl code goes here * * ## open a file using perl file open* * ## use dancer logger to output to console* * ## execute shell commands using system call of perl* *}* * * *ajax '/killprocess' => sub {* * get_stats;* * * *};* * * But it seems that the perl code is not getting executed, I do not have anything shown in my logger (console), am I doing something wrong over here ?
Thank You \amk
On Sun, Jul 7, 2013 at 12:38 PM, David Precious <davidp@preshweb.co.uk>wrote:
On Sat, 6 Jul 2013 17:59:21 -0700 Rik Brown <rik@rikbrown.co.uk> wrote:
Of course you could also do:
{ results => \@sendArray }
if you want your output to be a hash at the top level.
Hope this helps!
Rik's answer above is the course of action I'd typically recommend - because even if you just want the array for now, in future you may well decide you want to provide other information from that call, too (perhaps be able to return an error message as a key named 'error', or return the number of results found as a key named 'count', or return the average of all the values... etc) - so returning a hashref as Rik illustrates helps with future-proofing, as you could easily add more information to the response without breaking existing code.
-- David Precious ("bigpresh") <davidp@preshweb.co.uk> http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook www.preshweb.co.uk/cpan www.preshweb.co.uk/github
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
participants (3)
-
Anand Meher -
David Precious -
Rik Brown