0

There's a ton of these questions out there, but I am unable to find what is missing.

My JSP page:

    <button onclick="testAjax1()">Test Ajax 1</button>
    <button id="testAjax2">Test Ajax 2</button>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        // test 1
        function testAjax1() {
        $.ajax({
            type: "GET",
            url: "/ajax_test",
            success: function(response){
                console.log("Response for testAjax1: " + response);
            },
            error: function(e){
                console.log("Error: " + e);
            }
        });
        }
        
        // test 2
        $(document).on("click", "#testAjax2", function() {
            $.get("/ajax_test", function(response) {
                console.log("Response for testAjax2: " + response);
            });
        });
    </script>

My JAX-RS endpoint:

import javax.servlet.http.HttpServletResponse;

@Context
protected HttpServletResponse response;

@GET
@Path("/ajax_test")
@Consumes("application/x-www-form-urlencoded; charset=UTF-8")
@Produces({ "text/html" })
public void testAjax() throws IOException {
    System.out.println("/ajax_test reached");
    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write("response from server");
}

I have confirmed that both buttons result in my backend method being called (i.e. "/ajax_test reached" is printed to my local host console).

However, the response data to my ajax functions are undefined (i.e. "Response for testAjax1: undefined" or "Response for testAjax2: undefined" are printed to my chrome console).

What am I doing wrong?

Azianese
  • 554
  • 7
  • 21
  • Where is the variable `response` defined in the servlet? – dan1st Nov 11 '20 at 21:26
  • Sorry, should have included that in my post. I'll edit my post. Response is defined with: @Context protected HttpServletResponse response; – Azianese Nov 11 '20 at 21:28
  • 1
    You're confusing Servlets with JAX-RS. All these annotations are not part of the Servlet API but of the JAX-RS API. An example of a real Servlet can be found in this related Q&A: https://stackoverflow.com/q/4112686 When using the proper terminology and knowing what you're actually using, [finding](https://google.com/search?q=jax-rs+ajax) answers will be much easier. – BalusC Nov 11 '20 at 21:36
  • Thanks for the correction. That at least gives me something more to look into – Azianese Nov 11 '20 at 21:38
  • @BalusC Thanks! Your tip led me to https://stackoverflow.com/questions/50074831/ajax-request-from-jax-rs-web-service, which solved my issue. I'm not sure if I should mark this question as a duplicate, but if you create an answer, I'll accept it. I spent hours on this issue when the right terminology would have led me to the answer within 5 minutes :( – Azianese Nov 11 '20 at 21:49
  • You're welcome. No need to repeat answers if they already exist. – BalusC Nov 11 '20 at 21:52

0 Answers0