I am building an autosave feature for my Play app and when my JQuery runs, it returns a Bad Request error (Error 400).
EDIT: I updated my code by removing the parameter I was passing to the controller and followed the documentation here:
https://www.playframework.com/documentation/2.6.x/JavaJsonActions
Here is what is returned in the Chrome console:
addptp:326 POST http://localhost:9000/autosave 400 (Bad Request)
send @ jquery-3.2.1.min.js:4
ajax @ jquery-3.2.1.min.js:4
(anonymous) @ addptp:326
each @ jquery-3.2.1.min.js:2
each @ jquery-3.2.1.min.js:2
autosave @ addptp:325
(anonymous) @ addptp:188
My JQuery:
var timer;
var restURL = window.location.protocol + "//" + window.location.hostname + (window.location.port == "" ? "" : (":" + window.location.port));
$(document).ready(function() {
// JSON REST Autosave...
timer = setInterval(function() {
autosave();
}, 6000);
});
function autosave() {
alert("Autosave");
$('form').each(function() {
$.ajax({
type: "POST",
url: restURL + "/autosave",
data: $(this).serialize(),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
alert("Success!");
},
error: function(data) {
alert("Error!");
}
});
});
}
Here is the route in the routes file:
POST /autosave controllers.Application.restAutosaveAdultPTP()
Here is my Java function in my controller:
@BodyParser.Of(BodyParser.Json.class)
public Result restAutosaveAdultPTP() {
// Let's get the current request in JSON and parse...
JsonNode json = request().body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
} else {
return ok("json: " + json);
}
}
Based on the forum posts I have read, I have all the needed parameters for the AJAX call.
I appreciate the help.