Get JSON response from AJAX and put in HTML to be rendered in another JSP -


i have jsp in web application. jsp displays data json in grid.:

            <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">             <html>             <head>             <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />             <title>argo</title>             <link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />             <link rel="stylesheet" type="text/css" href="jtable/themes/metro/blue/jtable.min.css"/>             <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>             <script type="text/javascript" src="js/jquery-ui.js"></script>             <script type="text/javascript" src="jtable/jquery.jtable.min.js"></script>             <script type="text/javascript" src="js/argo.js"></script> <body><div id= "producttable"></div>                     $('#producttable').jtable({                         title: 'table of people',                         actions: {                            listaction: '/bsnet/test.html'                          },                          fields: {                             productid: {                                 title: "organization",                                 width:"20%"                             },                             productname: {                                 title: "role",                                 width: "10%"                             },                             unitcost: {                                 title: "status",                                 width: "20%"                             }                         }                     });                     $('#producttable').jtable('load');                 });             </script>             </html>    

i using jtable display contents in grid.
jtable expects json response. , expects response in format:

 {                "result":"ok",              "records":[                 {"productid":"fi-sw-01","productname":"koi","unitcost":10.00,"status":"p","listprice":36.50,"attr1":"large","itemid":"est-1"}}]} 

my web service not send "result":"ok" part of json response.

so decided embed html in between add response , send jtable jsp.

            html created:                  <html>                     <head>                         <script type="text/javascript" src="js/jquery-1.9.1.js"></script>                     </head>                     <body>                <!-- placing in body of html  -->                {"result":"ok","records":                          <script type="text/javascript">                         $(document).ready(function(){     // json file contains server response     // replaced web service url                          $.getjson("/test/data.json", function(data){     // adding response body                             $('body').append("<span>"+json.stringify(data)+"</span>}");                           });                         });                         </script>                     </body>                 </html>              when refer html in jsp adding part of url, entire html content like: <html><head>....             want json response or body content               {              "result":"ok",              "records":[                 {"productid":"fi-sw-01","productname":"koi","unitcost":10.00,"status":"p","listprice":36.50,"attr1":"large","itemid":"est-1"}]} 

from html in jsp. possible?
when hit html in browser, can see required response.
help???

ok, here simplest solution using jsp scriptlet:

put wrapper jsp (only this):

<%@ page import="java.net.url" %> <%@ page import="java.io.inputstream" %> <%@ page import="java.util.scanner" %> { "result":"ok", "records": <%     final string yourserviceurl = "your_full_url_with_http_and_stuff";     final string yourserviceencoding = "utf-8";      final inputstream stream = new url(yourserviceurl).openstream();     final string text = new scanner(stream, yourserviceencoding ).usedelimiter("\\a").next();     out.write(text); %> }  <%     response.setcontenttype("application/json"); %> 

you can add exception handing code generate "result: error" if reading stream has failed.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -