在Play Framework / Scala REST服务中接收POST提交

时间:2016-01-04 11:09:39

标签: scala playframework playframework-2.0

我已经在线查看,无法完成这项工作。我只需要接收从浏览器提交到服务器的POST字段。表单使用单个JSON对象提交。

这就是我所拥有的:

  case class Us (firstName: String, lastName: String)

  def applyUser = Action(BodyParsers.parse.json) {

    val userForm = Form(
      mapping(
        "first" -> text,
        "last" -> text
      )(Us.apply)(Us.unapply)
    )

    val json: JsValue = JsObject(Seq("ret" -> JsString("0")))
    Ok(json)

}

我收到以下错误:Expression of type Result doesn't conform to expected type (Request[JsValue]) => Result

此代码有什么问题?

1 个答案:

答案 0 :(得分:1)

Action apply方法需要一个函数而不是Result对象。请尝试以下方法:

case class Us (firstName: String, lastName: String)

  def applyUser = Action(BodyParsers.parse.json) { body => //This is the change required to your code

    val userForm = Form(
      mapping(
        "first" -> text,
        "last" -> text
      )(Us.apply)(Us.unapply)
    )

    val json: JsValue = JsObject(Seq("ret" -> JsString("0")))
    Ok(json)

}