java - Passing objects from servlets to client side(javascript) -
i'd know best way pass data servlet js code. solution came mind serialize object json string, pass request attribute jsp page , in js code , deserialize.
is there better approach this?
you generate json object <script>
tag within jsp syntax, have inline javascript browser deserialize on page load without needing grab request attribute. similar following within jsp page.
... <script> <% out.println("myjsonobj = " + myjsonobject.tostring()) %> //other javascript stuff... console.log(myjsonobj.key1); </script> ...
so rendered output/read in on browser (the actual json object dependent on serialize -- put jsonobject
of course):
... <script> myjsonobj = {key1:value1,array1:[val1,val2,val3]}; //other javascript stuff... console.log(myjsonobj.key1); </script> ...
another option ajax simple return json string servlet , receive return type being json (if using jquery ajax, otherwise json.parse(...) on xhr response).
Comments
Post a Comment