Play Framework JQuery AJAX REST Post调用返回Bad Request错误

我正在为我的Play应用程序构建自动保存function,当我的JQuery运行时,它会返回错误请求错误(错误400)。

编辑:我通过删除我传递给控制器​​的参数更新了我的代码,并按照此处的文档: https : //www.playframework.com/documentation/2.6.x/JavaJsonActions

以下是Chrome控制台中返回的内容:

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 

我的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!"); } }); }); } 

这是routes文件中的routes

 POST /autosave controllers.Application.restAutosaveAdultPTP() 

这是我的控制器中的Java函数:

 @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); } } 

根据我读过的论坛post,我有AJAX调用所需的所有参数。

我很感激帮助。