[dancer-users] CORS and dancer (1)

Tony Edwardson tony at edwardson.co.uk
Mon Apr 27 17:10:10 BST 2015


Hi All

I am working on implementing a REST api using dancer to be called from a 
website using ExtJs to get and send data as JSON.
As the dancer app will be on a different port (and possibly on a 
different machine) I am trying to setup CORS.
My initial attempt involved setting up shares in my Controller for each 
resource and method like this :-

package Controller::API;
use Dancer qw/ :syntax /;
use Dancer::Plugin::CORS;
.....
foreach my $resource (@resources) {
     get "/$resource" => sub {
          ........
     };
     get"/$resource/*" => sub {
........
     };
     # repeat for each method
     ........
     share "/$resource" =>
         origin => [ 'http://somewhere:port', ........ ],
        methods => [ qw/GET PUT POST DELETE OPTIONS/],
        credentials => 1;

share "/$resource/*" =>
         origin => [ 'http://somewhere:port', ........ ],
        methods => [ qw/GET PUT POST DELETE OPTIONS/],
        credentials => 1;
}

Which worked fine for get but when the extjs app tried a post or put to 
the api, it send OPTIONS first and I can't work out how to respond 
appropriately from the api to get it to send a post or put.

Then I tried to setup CORS via Plack middleware but can't get that to 
work either.
here is what I tried :-
in environments/development.yml
pack_middlewares:
     CrossOrigin:

Doing this I see 'add middleware CrossOrigin' in the logs but CORS 
doesn't work (even on Get).

Then I tried in my Controller:-
set plack_middlewares => [
     [ CrossOrigin => [ origins => '*', methods => ['GET', 'POST', 
'PUT', 'OPTIONS','DELETE']]]
];

Still no joy.

Anyone got any ideas ?

Cheers
Tony

BTW: I would have prefered to use Dancer2 but it appears that there is 
no CORS plugin for Dancer2


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150427/cdbde3f2/attachment.html>


More information about the dancer-users mailing list