#!/usr/bin/perl -w
use Mojolicious::Lite;
options '*' => sub { my $self = shift;
$self->res->headers->header('Access-Control-Allow-Origin' => '*'); $self->res->headers->header('Access-Control-Allow-Credentials' => 'true'); $self->res->headers->header('Access-Control-Allow-Methods' => 'GET, OPTIONS, POST, DELETE, PUT'); $self->res->headers->header('Access-Control-Allow-Headers' => 'Content-Type'); $self->res->headers->header('Access-Control-Max-Age' => '1728000');
$self->respond_to(any => { data => '', status => 200 });};
get '/data' => sub { my $self = shift;
print "GET found\n"; $self->render(text => 'ok');};
post '/data' => sub { my $self = shift;
print "\nPOST found\n"; $self->render(text => 'POST ok');};
app->secrets(['My very secret passphrase.']);
app->start;
$http({ url: "http://localhost:3000/data" , method: 'POST' , headers: { 'Content-Type': 'application/json' } , data: { payload : payload } }).then(function(response) { // Success ConsoleLog("$http POST success"); } , function(response) { ConsoleLog("$http POST failure"); });
curl --data "id=123" http://localhost:3000/data
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/data. This can be fixed by moving the resource to the same domain or enabling CORS.
We're sure this is something simple, but after a very long night and far too much coffee we've no idea what the problem is.
Any suggestions welcomed (including pack it all in and take up fishing).
Thanks
Rob
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
#!/usr/bin/perl -w
use Mojolicious::Lite;
options '*' => sub { my $self = shift;
$self->res->headers->header('Access-Control-Allow-Origin' => '*');
#$self->res->headers->header('Access-Control-Allow-Credentials' => 'true'); #$self->res->headers->header('Access-Control-Allow-Methods' => 'GET, OPTIONS, POST, DELETE, PUT'); #$self->res->headers->header('Access-Control-Allow-Headers' => 'Content-Type'); #$self->res->headers->header('Access-Control-Max-Age' => '1728000');
$self->respond_to(any => { data => '', status => 200 });};
get '/data' => sub { my $self = shift;
print "GET found\n"; $self->render(text => 'ok');};
post '/data' => sub { my $self = shift;
print "\nPOST 3 found\n";
$self->render(text => 'POST ok' );};
app->hook(after_dispatch => sub { my $c = shift;
$c->res->headers->header('Access-Control-Allow-Origin' => '*');
});
app->secrets(['My very secret passphrase.']);
app->start;
macpro:~ rwillett$ curl -v -X OPTIONS -H 'Origin: site.tld' -H 'Access-Control-Request-Method: POST' http://127.0.0.1:3000/data* Hostname was NOT found in DNS cache* Trying 127.0.0.1...* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)> OPTIONS /data HTTP/1.1> User-Agent: curl/7.37.1> Host: 127.0.0.1:3000> Accept: */*> Origin: site.tld> Access-Control-Request-Method: POST>< HTTP/1.1 204 No Content< Date: Wed, 29 Apr 2015 17:29:45 GMT< Content-Length: 0< Connection: keep-alive* Server Mojolicious (Perl) is not blacklisted< Server: Mojolicious (Perl)< Content-Type: text/html;charset=UTF-8<* Connection #0 to host 127.0.0.1 left intact
[Wed Apr 29 18:29:27 2015] [debug] OPTIONS "/data"[Wed Apr 29 18:29:27 2015] [debug] Routing to a callback[Wed Apr 29 18:29:27 2015] [debug] 204 No Content (0.000504s, 1984.127/s)[Wed Apr 29 18:29:45 2015] [debug] OPTIONS "/data"[Wed Apr 29 18:29:45 2015] [debug] Routing to a callback[Wed Apr 29 18:29:45 2015] [debug] 204 No Content (0.000465s, 2150.538/s)
[Wed Apr 29 18:31:17 2015] [debug] POST "/data"[Wed Apr 29 18:31:17 2015] [debug] Template "not_found.development.html.ep" not found[Wed Apr 29 18:31:17 2015] [debug] Template "not_found.html.ep" not found[Wed Apr 29 18:31:17 2015] [debug] Rendering cached inline template "fd403ab55a4c875e35b42428816134c7"[Wed Apr 29 18:31:17 2015] [debug] Rendering cached inline template "dd85456cf9f44e1e43bebae130a7f9df"[Wed Apr 29 18:31:17 2015] [debug] 404 Not Found (0.004456s, 224.417/s)
[Wed Apr 29 18:32:11 2015] [debug] GET "/data"[Wed Apr 29 18:32:11 2015] [debug] Template "not_found.development.html.ep" not found[Wed Apr 29 18:32:11 2015] [debug] Template "not_found.html.ep" not found[Wed Apr 29 18:32:11 2015] [debug] Rendering cached inline template "fd403ab55a4c875e35b42428816134c7"[Wed Apr 29 18:32:11 2015] [debug] Rendering cached inline template "dd85456cf9f44e1e43bebae130a7f9df"[Wed Apr 29 18:32:11 2015] [debug] 404 Not Found (0.004497s, 222.370/s)
--
WBR, Alex.
- Request URL:http://localhost:3002/users/login
- Request Method:OPTIONS
- Status Code:404 Not Found
- Remote Address:
- HTTP/1.1 404 Not Found
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization
Access-Control-Allow-Credentials: true
Date: Wed, 20 Apr 2016 21:11:06 GMT
Access-Control-Allow-Origin: http://localhost:3000
Access-Control-Allow-Methods: GET, OPTIONS, POST, DELETE, PUT
Content-Length: 17915
Access-Control-Max-Age: 1728000
Content-Type: text/html;charset=UTF-8
Server: Mojolicious (Perl)
- OPTIONS /users/login HTTP/1.1
Host: localhost:3002
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
Access-Control-Request-Headers: accept, authorization, content-type
Accept: */*
Referer: http://localhost:3000/?adaptor=localhost:3002&api=localhost:8080
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
use Mojolicious::Lite;use Mojo::Util 'secure_compare';
options '*' => sub { my $self = shift;
my $path = $self->req->url->to_abs->path;
say "PATH: $path";
$self->res->headers->header('Access-Control-Allow-Origin' => $self->req->headers->origin);
$self->res->headers->header('Access-Control-Allow-Credentials' => 'true');
$self->res->headers->header('Access-Control-Allow-Methods' => 'GET, OPTIONS, POST, DELETE, PUT');
$self->res->headers->header('Access-Control-Allow-Headers' => 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
$self->res->headers->header('Access-Control-Max-Age' => '1728000');
$self->respond_to(any => { $path => '', status => 200 });};
post '/users/login' => sub { my $c = shift;
say "Authentication attempt...";
# Check for username "Bender" and password "rocks" return $c->render(text => 'Hello Bender!') if secure_compare $c->req->url->to_abs->userinfo, 'Bender:rocks';
# Require authentication $c->res->headers->www_authenticate('Basic'); $c->render(text => 'Authentication required!', status => 401);
};
app->hook(after_dispatch => sub { my $c = shift;
my $headers = $c->req->headers; my $origin = $headers->origin; say "Origin: $origin";
$c->res->headers->header('Access-Control-Allow-Origin' => $origin);
});
app->secrets(['My very secret passphrase.']);app->start;
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
Pleased it works