簡體   English   中英

無法從Servlet Java在Ajax中記錄Json響應?

[英]Unable to log Json response in Ajax from servlet Java?

我正在嘗試在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>

您將需要編寫一個回調,當您的ajax調用完成時將觸發該回調,請嘗試如下操作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM