无法从servlet Java中记录Ajax中的Json响应?

时间:2017-07-14 08:05:06

标签: java jquery json ajax servlets

我试图在我的ajax调用中记录来自servlet的json响应。现在,这就是问题,我尝试在我的ajax中尝试记录字符串响应,事情似乎工作正常,但后来我尝试记录json响应,我在控制台中看不到任何内容。我想知道我是否错过了某些东西。我尝试从POSTMAN调用我的servlet并得到了正确的json响应。

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>

1 个答案:

答案 0 :(得分:0)

您需要编写一个回调函数,这将在您的ajax调用完成后触发,请尝试如下

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