java - Play 2.2 Return Json Response within Web Application -
is there way make request restfully service on same application , display it's response too? i've created ui has form parameters fill out. when user clicks submit, i'd have response embedded in same page, displaying user json. i'd able called externally of course, restful api.
i can define in routes file path return json, i'm not sure how consume application itself.
i hope clear. i'd happy provide more details if necessary.
ok. first of all, let's consider have route , controller produce json response us.
get /foo controllers.foocontroller.foo object foocontroller extends controller { ..... implicit val foowrites = json.writes[foo] def foo = action { ok(json.tojson(foo)).as("application/json") }
}
then can use ajax our page response:
<script> ...... $.get("@routes.foocontroller.foo") .done(function(data){ //do recieved data }); </script>
or if want consume data within play app,you may use ws lib. example.
object foocontroller extends controller {
.... def fooconsumercontroller = action.async { val foojsonresultfuture = ws.url("http://localhost:9000/foo").get().map(_.body) ..... foojsonresultfuture.map { json => // result ok(.....) } } }
it's not clear question, behavior want achieve, hope figure out directions.
Comments
Post a Comment