【发布时间】:2015-06-24 16:13:47
【问题描述】:
我正在尝试将表单作为 JSON 对象提交,因为我想创建一个带有播放功能的 REST API。
我遇到的问题是 Play 告诉我这不是有效的 JSON。
我的表格代码:
@(form : Form[Product]) @main("Create Form"){
@helper.form(routes.Products.createProduct, 'enctype -> "application/json"){
@helper.inputText(form("name"))
<button>Commit</button>
} }
控制器代码:
// Read JSON an tell if it has a name Path
@BodyParser.Of(BodyParser.TolerantJson.class)
public static Result createProduct() {
JsonNode json = request().body().asJson();
String name = json.findPath("name").textValue();
if (name == null) {
return badRequest("Not JSON");
} else {
return ok(name);
}
}
最好的方法是什么?阅读有关使用 Ajax 提交的信息,但因为我是 play 新手,所以我不知道如何使用 Play 的表单语法来做到这一点。
【问题讨论】:
标签: javascript jquery json forms playframework