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

Receiving POST submission in Play Framework/Scala REST service

我在网上看过,无法完成这项工作。我只需要接收从浏览器提交到服务器的 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

这段代码有什么问题?

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)

}