I have a problem regarding sending a json data to Play Controller.
seach.scala.html
$.ajax({
type : "POST",
dataType: 'json',
data: {
'filter': "John Portella"
},
url : "@routes.Search.findPag()",
success: function(data){
console.log(data);
}
});
return false;
Controller : POST /find/findPag Search.findPag()
public static Result findPag(){
JsonNode json = request().body().asJson();
return ok();
}
Debugging I get json = null . Which you think may be the problem?. Thank.
You'll have to stringify the data. As it is right now I think that .toString()
will be called on the data object and that is not something that can be correctly parsed as JSON on the server side.
var d = { 'filter': "John Portella" };
$.ajax({
type : "POST",
dataType: 'json',
data: JSON.stringify(d),
url : "@routes.Search.findPag()",
success: function(data){
console.log(data);
}
});
You'll have to "contentType" the data.
var d = { 'filter': "John Portella" };
$.ajax({
type : "POST",
dataType: 'json',
data: JSON.stringify(d),
contentType: "application/json; charset=utf-8",
url : "@routes.Search.findPag()",
success: function(data){
console.log(data);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With