【发布时间】:2014-07-13 17:01:44
【问题描述】:
我的框架是 playframework2.2.2。 我使用 jquery 发送 ajax json: 我的 javascript(客户端)部分:
$("#newapply" ).click(function(){
var htmlrespone=$.ajax({
type : 'post',
url : ' @routes.CommonController.postdata ',
data : JSON.stringify({"userid": "111233456"}),
sucess:function(){
$("#newapply" ).html("already apply");
$("#newapply" ).prop("enable",false);
},
contentType: 'appliction/json'
});
})
我的萤火虫的帖子字符串是:
{"userid":"111233456"}
我的服务器部分收到的帖子是:
def postdata = Action { request =>
val body: AnyContent = request.body
val textBody: Option[JsValue] = body.asJson //here I always get None I donot know why
textBody.map{json=>
Ok("Got:"+(json\"userid").as[String])
}.getOrElse{
BadRequest("Expecting text/plain request body")
}
}
这里我总是得到 BadRequest,因为 getOrElse 语句总是 None。 我认为帖子应该是 json 对象或 JsValue。 但为什么我在这里没有?如何从 body 中获取 JsValue?
【问题讨论】:
标签: ajax json scala playframework-2.0 playframework-2.2