play framework routes trouble (400 bad request)

2019-02-23 09:27发布

问题:

I'm having trouble making a POST to the play framework - this may not even be Play related as much as HTTP related.

$.ajax({
    type:'POST',
    url:'http://localhost:9000/start',
    data: {
        myJson:JSON.stringify(arg)
    }
}).done(function(data) {
    console.log(data);
});

where arg is an array of strings, ie: ['a', 'b', 'c']

The route I'm trying to use to capture this is:

POST    /start      controllers.Application.startIt(myJson)

What am I doing wrong? As of right now (if the route is capturing correctly), that function will never return a 400. There is no output to the Play console, only javascript:

POST http://localhost:9000/start 400 (Bad Request)

回答1:

The documentation explains when a BadRequest error code is returned by the framework. The problem comes from your router file. You define a route /start which will trigger a method call startIt, but the method has an argument and the framework does not know which value it should pass.

To handle correctly JSON requests, have a look to the dedicated part of the documentation.