[dancer-users] ajax plugin, serialize a 1D perl array to JSON

Rik Brown rik at rikbrown.co.uk
Sun Jul 7 18:12:34 BST 2013


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 at 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 at preshweb.co.uk>wrote:
>
>> On Sat, 6 Jul 2013 17:59:21 -0700
>> Rik Brown <rik at 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 at 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 at dancer.pm
>> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>>
>
>
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20130707/4a59e47e/attachment.htm>


More information about the dancer-users mailing list