Dancer2::Plugin::Ajax - a plugin for adding Ajax route handlers
version 0.400000
package MyWebApp; use Dancer2; use Dancer2::Plugin::Ajax; # For GET / POST ajax '/check_for_update' => sub { my $self = shift; # ... some Ajax code }; # For all valid HTTP methods ajax ['get', 'post', ... ] => '/check_for_more' => sub { my $self = shift; # ... some Ajax code }; dance;
The ajax keyword which is exported by this plugin allows you to define a route handler optimized for Ajax queries.
ajax
The route handler code will be compiled to behave like the following:
Pass if the request header X-Requested-With doesn't equal XMLHttpRequest
Disable the layout
The action built matches POST / GET requests by default. This can be extended by passing it an ArrayRef of allowed HTTP methods.
The route handler gets the Dancer $self object passed in, just like any other Dancer2 route handler. You can use this to inspect the request data.
$self
ajax '/check_for_update' => sub { my $self = shift; my $method = $self->app->request->method; # ... }
By default the plugin will use a content-type of 'text/xml', but this can be overridden with the plugin setting content_type.
content_type
Here is an example to use JSON:
plugins: Ajax: content_type: 'application/json'
Dancer Core Developers
This software is copyright (c) 2016 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Dancer2::Plugin::Ajax, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer2::Plugin::Ajax
CPAN shell
perl -MCPAN -e shell install Dancer2::Plugin::Ajax
For more information on module installation, please visit the detailed CPAN module installation guide.