返回的值为undefined
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path=request.getContextPath(); String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>图书查询</title> </head> <body> <center> <h1>图书查询</h1> <p> 图书ID:<input type="text" name="bookID"> 图书名:<input type="text" name="bookName"> 分类:<input type="text" name="catgoryName"> <input type="submit" value="查询" id="search"> </p> <hr> <table width="800px" cellspacing="0px" cellpadding="0px" border="1px"> <thead> <tr> <th>图书ID</th> <th>书名</th> <th>分类</th> <th>价格</th> <th>描述</th> </tr> </thead> <tbody id="cont"> <c:forEach var="m" items="${books}"> <tr> <td>${m.id }</td> <td>${m.name }</td> <td>${m.classname }</td> <td>${m.price }</td> <td>${m.description }</td> </tr> </c:forEach> </tbody> </table> </center> <script type="text/javascript" src="<%=basePath%>js/jquery-3.3.1.js"></script> <script type="text/javascript"> $("#search").click(function(){ //单击查询按钮的时候触发ajax事件 $.ajax({ url:"<%=basePath%>SelectBookServlet" , type:"post", data:{ bookID:$("input[name=bookID]").val(), bookName:$("input[name=bookName]").val(), catgoryName:$("input[name=catgoryName]").val() }, dataType:"json", success:function(result){//result里封装了servlet给ajax返回的结果 var list= eval(result); console.log(list); var s = null; for (var i in list){ var id = list[i].id; var bookName = list[i].name; var bookCategory = list[i].classname; var price = list[i].price; var description = list[i].description; s = s+ "<tr><td>" + id +"</td><td>"+bookName+"</td><td>"+bookCategory+"</td><td>"+price+"</td><td>"+ description+ "</td></tr>"; $("#cont>tr").remove(); } $("#cont").html(s); } }); }); </script> </body> </html>
package wust.model; public class Book { private String id; private String name; private String classname; private String price; private String description; public Book() { } public Book(String id, String name, String classname, String price, String description) { super(); this.id = id; this.name = name; this.classname = classname; this.price = price; this.description = description; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", classname=" + classname + ", price=" + price + ", description=" + description + "]"; } }
package wust.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONArray; import wust.dao.LibDaoImpl; import wust.model.Book; /** * Servlet implementation class SelectBookServlet */ @WebServlet("/SelectBookServlet") public class SelectBookServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SelectBookServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LibDaoImpl libdaoimpl=new LibDaoImpl(); String bookID=request.getParameter("bookID"); String bookName=request.getParameter("bookName"); String catgoryName=request.getParameter("catgoryName"); if(bookID==null&&bookName==null&&catgoryName==null) { List<Book> books=libdaoimpl.getBooksByCondition(null,null,null); request.setAttribute("books", books); request.getRequestDispatcher("/showBooks.jsp").forward(request, response); } else { List<Book> books=libdaoimpl.getBooksByCondition(bookID, bookName, catgoryName); JSONArray jsonArray = new JSONArray(books); System.out.println(jsonArray); response.getOutputStream().write(jsonArray.toString().getBytes("utf-8")); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); doGet(request, response); } }
json能正确返回,但是显示是undefined。
0
收起
正在回答 回答被采纳积分+1
2回答
好帮手慕阿莹
2019-02-26 10:01:38
老师用同学的代码测试着没有问题:
因为老师没有你的LibDaoImpl类,这里自己写了一个集合模拟查询结果。
建议同学试试自己写一个固定的集合,看看是否能读取出来呢?
老师测试的代码如下,同学也可以拿去试一下,如果这个没有问题,可能就是LibDaoImpl返回值的格式出了问题。建议同学检查一下。
package wust.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONArray; import wust.model.Book; /** * Servlet implementation class SelectBookServlet */ @WebServlet("/SelectBookServlet") public class SelectBookServlet extends HttpServlet { private static final long serialVersionUID = 1L; public SelectBookServlet() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String bookID=request.getParameter("bookID"); String bookName=request.getParameter("bookName"); String catgoryName=request.getParameter("catgoryName"); if(bookID==null&&bookName==null&&catgoryName==null) { List<Book> books=new ArrayList<Book>(); Book book = new Book("1","2","3","4","5"); Book book1 = new Book("你好","22","33","44","55"); Book book2 = new Book("111","222","333","444","555"); books.add(book); books.add(book1); books.add(book2); request.setAttribute("books", books); request.getRequestDispatcher("/showBooks.jsp").forward(request, response); } else { List<Book> books=new ArrayList<Book>(); Book book = new Book("1","2","3","4","5"); Book book1 = new Book("你好","22","33","44","55"); books.add(book); books.add(book1); JSONArray jsonArray = new JSONArray(books); System.out.println(jsonArray); response.getOutputStream().write(jsonArray.toString().getBytes("utf-8")); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); doGet(request, response); } }
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星