Play框架 - 表单不绑定到嵌套元组

时间:2014-11-18 18:27:37

标签: javascript scala playframework-2.0 datatables jquery-datatables

我创建了一个Form以绑定到datatables请求:

val datatableForm = Form(
  tuple(
    "draw" -> number,
    "start" -> number,
    "length" -> number,
    "search" -> tuple(
        "value" -> text,
        "regex" -> boolean
    ),
    "columns" -> seq(tuple(
        "data" -> number,
        "name" -> text,
        "orderable" -> boolean,
        "search" -> tuple(
            "value" -> text,
            "regex" -> boolean
        )
      )
    ),
    "order" -> seq(tuple(
        "column" -> number,
        "dir" -> text
      )
    )
  )
)

在我的请求中,我致电val form = datatableForm.bindFromRequest。这适用于drawstartlength变量,但对searchcolumnsorder变量失败。例如,当我不使用嵌套元组时,我可以调用datatableForm.bindFromRequest.get并返回一个Some对象,但是当我添加嵌套元组并调用get时,它会返回{{} 1}}对象。

从datatables javascript对象发送的数据是:

None

这是请求正文中的数据:

{
  "draw": 1,
  "columns": [
    {
      "data": 0,
      "name": "",
      "searchable": true,
      "orderable": true,
      "search": {
        "value": "",
        "regex": false
      }
    },
    {
      "data": 1,
      "name": "",
      "searchable": true,
      "orderable": true,
      "search": {
        "value": "",
        "regex": false
      }
    }
  ],
  "order": [
    {
      "column": 0,
      "dir": "asc"
    }
  ],
  "start": 0,
  "length": 10,
  "search": {
    "value": "",
    "regex": false
  }
}

如何让Form绑定到嵌套元组?

1 个答案:

答案 0 :(得分:4)

我看到同样的错误。问题是请求未作为json发送。务必将contentType设置为json

如果您使用的是jQuery,则文档位于here,了解如何执行此操作。

基本上是这样的:

$.ajax({
  type: "POST",
  url: youUrl,
  data : data,
  contentType: "application/json"
});