我已经在线查看,无法完成这项工作。我只需要接收从浏览器提交到服务器的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
此代码有什么问题?
答案 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)
}