i am new to Perl and Mojo and i've got one problem by receiving POST-Data from Angular:
My AngularCode is
var datainput = JSON.stringify({"test":"orcl"});
$http.post('http://localhost/perltest/perltest.pl/post', datainput)
.success(function(data, status, headers, config) {
console.log("post geschickt");
console.log(headers());
console.log(data);
console.log("data back: " + JSON.stringify(data));
alert(JSON.stringify(data));
})
My Mojo-Sub looks like:
post '/post' => sub {
my $self = shift;
my $json = $self->req->json;
print header(-type => "text/html");
print Dumper($json->{test});
};
app->start;
The Result i get is: $VAR1 = undef; Content-Length: 0 Status: 404 Not Found Date: Fri, 20 Jan 2017 09:49:57 GMT
What is wrong? It seems to me that $json = $self->req->json is not getting the JSON-String from POST ?
The
404 Not Found
indicates the resource is not found. Please double check if your application is available underhttp://localhost/perltest/perltest.pl/post
.You should not use print(), because it's not reliable (sometimes it works and sometimes not). If you want to log text into your console, please use $self->app->log->debug(). Mojolicious also has $self->dumper, you do not need to include the external module
Data::Dumper
.Check the data which is actually send. You can use a service like http://requestb.in/. If you recieve correct JSON; I would strongly expect the URL is not correct (see point 1.)