从 Servlet 解析 jquery 中的 jsonarray 时出错?



我正在尝试在我的jquery函数中读取jsonarray。发生了一些奇怪的事情。不是在我的成功函数中显示数据,而是在我的错误函数中接收数据。我完全不知道为什么它会以这种方式行事。任何人都可以指导我一下。我正在获取错误.响应文本中的数据。这太奇怪了。

家服务员.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
JSONObject job=new JSONObject(); //create a JSON Object obj.
JSONArray jArray = new JSONArray();
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");
job.put("value1", n);
job.put("value2", nm);
jArray.put(job); 



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

}
con.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}

首页.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>
<style>
.vertical-menu {
width: 200px;
}
.vertical-menu a {
background-color: #eee;
color: black;
display: block;
padding: 12px;
text-decoration: none;
}
.vertical-menu a:hover {
background-color: #ccc;
}
.vertical-menu a.active {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<form>
<div class="vertical-menu">
</div>
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...
//event.preventDefault();
$.ajax({
type: "GET",
url: "HomeServlet",
success: function(data) {
console.log("Entered");
$.each(data, function(key, value) {
console.log(value.value1); //alerting the values set in the JSONObject of the Sevlet.
console.log(value.value2);
})

},
error: function(error) { 
console.log("Entered err",error.responseText);
},
//dataType: "json",
contentType : "application/json"
});
return false;
});
</script>
</body>
</html>
String n = rs.getString("apiname");
String nm = rs.getString("apiendpoint");
Add data to jsonArray
JSONArray jArray = new JSONArray();
jArray.add(n);
jArray.add(nm);
Put jArray to Json Object
JSONObject job=new JSONObject();
job.put("Key",jArray);
Return JsonObject to response
return job.toString();
Try this see if it's Working......this is the way i do, may be it will help you

最新更新