<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Hi All<br>
<br>
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.<br>
As the dancer app will be on a different port (and possibly on a
different machine) I am trying to setup CORS.<br>
My initial attempt involved setting up shares in my Controller for
each resource and method like this :-<br>
<br>
<font color="#6600cc">package Controller::API;<br>
use Dancer qw/ :syntax /;<br>
use Dancer::Plugin::CORS;<br>
.....<br>
foreach my $resource (@resources) {<br>
get "/$resource" => sub {<br>
........<br>
};<br>
</font> get<font color="#6600cc"> "/$resource/*" => sub {<br>
........<br>
};<br>
# repeat for each method<br>
........<br>
share "/$resource" => <br>
origin => [ 'http://somewhere:port', ........ ],<br>
methods => [ qw/GET PUT POST DELETE OPTIONS/],<br>
credentials => 1;<br>
</font> <br>
<font color="#6600cc"><font color="#6600cc">share
"/$resource/*" => <br>
origin => [ 'http://somewhere:port', ........ ],<br>
methods => [ qw/GET PUT POST DELETE OPTIONS/],<br>
credentials => 1;</font><br>
}<br>
<br>
<font color="#000000">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.<br>
<br>
Then I tried to setup CORS via Plack middleware but can't get
that to work either.<br>
here is what I tried :-<br>
in environments/development.yml<br>
pack_middlewares:<br>
CrossOrigin:<br>
<br>
Doing this I see 'add middleware CrossOrigin' in the logs but
CORS doesn't work (even on Get).<br>
<br>
Then I tried in my Controller:-<br>
set plack_middlewares => [<br>
[ CrossOrigin => [ origins => '*', methods =>
['GET', 'POST', 'PUT', 'OPTIONS','DELETE']]]<br>
];<br>
<br>
Still no joy.<br>
<br>
Anyone got any ideas ? <br>
<br>
Cheers<br>
Tony<br>
<br>
BTW: I would have prefered to use Dancer2 but it appears that
there is no CORS plugin for Dancer2<br>
<br>
<br>
</font></font>
</body>
</html>