I'm trying to create a graph using data retrieved from a servlet accessing an EJB on a Glassfish server. Before implementing the graph, I was testing the servlet, so I used a javascript ajax call to retrieve the data, writing the result of the servlet (a JSON formatted string) to a div (id="sub-title").
The result is that nothing appears on the screen. Also, using the tools from the browser to inspect the result of the ajax call, a null string seems to be returned and the url seems to be correct. Actually I put a breakpoint in the javascript line including the call to jquery.html(...) function and the content of the data variable is showed empty.
The weird thing is that if I put the complete url on the browser, the JSON string is returned correctly.
It shouldn't be a cross-scripting problem, as I don't access an external server. Any hint?
The servlet:
/**
* Servlet implementation class RetrieveHistory
*/
@WebServlet("/history")
public class RetrieveHistory extends HttpServlet {
private static final long serialVersionUID = 1L;
List<Measure> measures;
@EJB
MeasureEJB measureEjb;
/**
* @see HttpServlet#HttpServlet()
*/
public RetrieveHistory() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
measures = measureEjb.findMeasures();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(new Gson().toJson(measures));
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
The javascript
<script>
$.ajax({
url: 'history',
success: function(data) {
$('#sub-title').html(data);
}
});
</script>
The url of the called servlet
http://localhost:8080/Smarthome-0.0.1-SNAPSHOT/history
The url of the calling servlet
http://localhost:8080/Smarthome-0.0.1-SNAPSHOT/dashboard