0

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
sthor69
  • 638
  • 1
  • 10
  • 25
  • Does the browser call the same URL when executing AJAX (i.e. have you debugged it using browser's developer tools)? What is the URL of the page with your AJAX code? – Jozef Chocholacek Sep 20 '16 at 07:07
  • Yes, the url is the same, and the preview in the 'network' tab of the developer tool of Chrome is correct (not empty). But the preview of the variable 'data' in the javascript code (I put a breakpoint) is null. The url of the page with ajax code is the same of the called servelt but with 'dashboard' replacing 'history' – sthor69 Sep 20 '16 at 07:12
  • Are you sure about the URL, according your code the url you are using is `history` – ianaya89 Sep 20 '16 at 07:17
  • @sthor69 Have you tried to use different browser? I have occasionally experienced some weird behavior with interpreting JSON - sometimes it worked everywhere except in IE, another time there was problem in Safari, etc. – Jozef Chocholacek Sep 20 '16 at 07:22
  • @Jozef I tried with Firefox and the result is the same – sthor69 Sep 20 '16 at 07:26
  • @ianaya89 the url of the page including the ajax call is http://localhost:8080/Smarthome-0.0.1-SNAPSHOT/dashboard while the one to be called is http://localhost:8080/Smarthome-0.0.1-SNAPSHOT/history. As I told Jozef, in the 'network' statistics of the developer tools of Chrome I see that the correct page is called by the ajax call – sthor69 Sep 20 '16 at 07:29
  • Can you access the server/ is the server at localhost running and serving anything? – Rik Sep 20 '16 at 08:11
  • @Rik not sure to understand. I tried with 'localhost:8080' and I see the main page of the Glassfish server (the one with 'Your server is running' message) – sthor69 Sep 20 '16 at 08:17
  • Sounds like it is running. What about doing a curl request to the history page `curl -I -X GET http://localhost:8080/.../history`. Does that give a response code that suggests the server is running or do you get a 404 – Rik Sep 20 '16 at 08:20
  • This is the result: $ curl -I -X GET http://localhost:8080/Smarthome-0.0.1-SNAPSHOT/history HTTP/1.1 200 OK X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.0 Java/Oracle Corporation/1.8) Server: GlassFish Server Open Source Edition 4.0 Content-Type: application/json;charset=UTF-8 Date: Tue, 20 Sep 2016 08:31:30 GMT Content-Length: 71 – sthor69 Sep 20 '16 at 08:32
  • So the server is running. Try implementing the `complete` callback or add the `error` callback so you can see if anything goes wrong. Maybe you can extend your `success(data)` with textstatus and jqXHR `success(data,textStatus,jqXHR)` so you can do some more debugging client side [jquery Ajax reference](http://api.jquery.com/jquery.ajax/). Also you can add logging server side to see how and if the request was handled (i.e. Write data that should be returned to log to see if it is correctly handled/retrieved) – Rik Sep 20 '16 at 13:43
  • The issue faded away without any action from my part. At least, any conscious action. Difficult to say what was going on. Thanks for helping – sthor69 Sep 20 '16 at 14:37
  • Great that it works! Bummer that you don't know why, but that won't be the last time :) – Rik Sep 20 '16 at 16:58

0 Answers0