jquery ajax post rest,Play Framework JQuery AJAX REST Post call returns

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值