简体   繁体   中英

Unable to log Json response in Ajax from servlet Java?

I am trying to log a json response from my servlet in my ajax call. Now, here is the thing, I tried experimenting with logging string response in my ajax and things seemed to work fine but then I try to log the json response, I see nothing in my console. I am wondering if I am missing out on something.I tried calling my servlet from POSTMAN and I got the proper json response.

HomeServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        Map<String, String> options = new LinkedHashMap<>();

          String text = "some text";
         PrintWriter out = response.getWriter();
         response.setContentType("text/html");
         try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost/apiprovider","root","");
             Statement stmt = con.createStatement();
             ResultSet rs = stmt.executeQuery("select * from apiinfo");
            // out.println("<table border=1 width=50% height=50%>");
            // out.println("<tr><th>EmpId</th><th>EmpName</th><th>Salary</th><tr>");
             while (rs.next()) {
                 String n = rs.getString("apiname");
                 String nm = rs.getString("apiendpoint");

                 options.put("value1", n);
                 options.put("value2", nm);
                 String json = new Gson().toJson(options);





                response.setContentType("application/json");
               response.setCharacterEncoding("UTF-8");
                 response.getWriter().write(json);

                // out.println("<tr><td>" + n + "</td><td>" + nm + "</td><td>" + s + "</td></tr>"); 
             }
           //  out.println("</table>");
            // out.println("</html></body>");
             con.close();
            }
             catch (Exception e) {
             out.println("error");
         }
     }

Home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<div id="ajaxresponse">

</div>
<form>
 API Name:<br>
  <input type="text" id = "apiname" name="apiname">
   API ENDPOINT:<br>
  <input type="text" id ="apiendpoint" name="apiendpoint">
  <br>
  API VERSION:<br>
  <input type="text" id="apiversion" name="apiversion">
   ACCESSIBLE:<br>
  <input type="checkbox" name="source" value="internet"> Internet<br>
    <input type="checkbox" name="source" value="vpn"> VPN<br>
 <!-- 
  <br><br>
  <input type="submit" formaction="Home" method="post" value="Submit"> -->
  <br>
    <input type="submit" id="check" name="check" value="Check">

</form> 
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(document).on("click", "#check", function() { // When HTML DOM "click" event is invoked on element with ID "somebutton", execute the following function...
   console.log("I was clicked");
    $.get("HomeServlet", function(responseText) { 
        console.log("response",responseText);

    // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
    });
});
</script>
</body>
</html>

You will need to write a callback, this will be fired when your ajax call completes, try like below

function success(data) {
console.log( data );
}
function failure(err) {
console.error( err );
}
ajax( "http://some.url.1", success, failure );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM