can't retrieve Gson Object on a javascript variable in my jsp
can't retrieve Gson Object on a javascript variable in my jsp
I tried to populate the DataTables with an object that contain data and also the columns specifications (name, type...). The Object is send from the servlet to the jsp and I can see with the debug mode of Netbeans that the data are on the servlet but can't retrieve the Gson on the jsp (I want to retrieve the data from servlet in a javascript variable) and then populate the dataTables with these data... I tried many ways these last few days but it still doesn't work so I'm asking your help...
Here is my servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
DataService ds = new DataService();
Collection licencesList = null;
try {
licencesList = ds.getLicences();
} catch (ObjectNotFoundException ex) {
Logger.getLogger(LicenceServlet.class.getName()).log(Level.SEVERE, null, ex);
}
DataTables dt = LicenceDatatables.getMainObject(licencesList);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(dt);
out.print(json);//
out.flush();
out.close();
I retrieve well the data from mysql on the servlet and send it as you can see to the client with the "out.print(json)" wich contain a DataTables object with the data inside and the names, types etc... of the columns.
Now here is my jsp:
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-1.8.1.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/r/bs-3.3.5/jqc-1.11.3,dt-1.10.8/datatables.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/bs-3.3.5/jq-2.1.4,dt-1.10.8/datatables.min.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css"/>
<script src="js/generateDataTables.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type:'GET',
url:'http://localhost:8080//myurl/licences',
headers: {
Accept: "application/json; charset=utf-8",
"Content-Type" : "application/json; charset=utf-8"
},
success: function (dt) {
var dataTablesObj = $.parseJSON(dt);
alert(dt.columns);
}
})
});
</script>
</body>
</html>
Here what I want for now is to retrieve the Gson data from the servlet on a javascript variable in the jsp... Once I'll achieve to do that I think I'll be able to populate the dataTables.
Answers
You are probably best asking this in StackOverflow or some JSP specific forum. If you can't get JSON or GSON back from the server, that is a server-side issue and not one I can really help with (since I don't know JSP!).
Allan
Ok, I fixed the problem.
JQuery parse directly the JSon because my ContentType was "application/json". So dt was already a Javascript object...