请查看为什么ajax不能接受json返回的数据
烦请老师检查代码:我在searchbook 的servlet中可以打印出搜索的结果,说明servlet本身应该没错,不明白为什么json返回的数值取不出来,还有怎么打印ajax的数据,便于debug?
用flag尝试测试也不能跳转页面。
这是我的searchbook jsp的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | < body > < center > < h1 >图书查询</ h1 > < p > 图书ID:< input type = "text" name = "bookID" > 图书名:< input type = "text" name = "bookName" > 分类:< input type = "text" name = "catgoryName" > < input type = "button" 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 items = "${booklist }" var = "book" > < tr > < td >${book.getId()}</ td > < td >${book.getBookName()}</ td > < td >${book.getCatgoryName()}</ td > < td >${book.getPrice()}</ td > < td >${book.getDescride()}</ td > </ tr > </ c:forEach > </ tbody > </ table > </ center > </ body > < script type = "text/javascript" > $("#search").click(function(){ //单击查询按钮的时候触发ajax事件 $.ajax({ url:"<%=basePath%>/searchBook", 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 flag=result.flag; if(flag==true) { //System.out.println("json返回成功!"); response.sendRedirect(request.getContextPath()+"/BookManage/showBooks.jsp"); }*/ var list= eval(result); var s = null; for (var i in list){ var id = list[i].id; var bookName = list[i].bookname; var bookCategory = list[i].bookCategory; 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 > |
这是我的searchbook servlet代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class searchBook extends HttpServlet { public List<Book> booklist; public List<Book> result; 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" ); //System.out.println("bookID "+bookID+"bookName "+bookName+" catgoryName "+catgoryName); if (request.getSession().getAttribute( "booklist" )!= null ) { booklist= (List<Book>) request.getSession().getAttribute( "booklist" ); } result= new ArrayList<>(); for (Book b: booklist) { if (b.getBookname().equals(bookName)||b.getCatgoryName().equals(catgoryName)||(b.getId()+ "" ).equals(bookID)) result.add(b); } for (Book b:result) { System.out.println(b.toString()); } //JSONObject jsonobject= new JSONObject("{flag:true}"); JSONArray jsonarray= new JSONArray(result); //response.getOutputStream().write(jsonobject.toString().getBytes("UTF-8")); response.getOutputStream().write(jsonarray.toString().getBytes( "UTF-8" )); //response.sendRedirect(request.getContextPath()+"/BookManage/searchBooks.jsp"); } |
1
收起
正在回答
1回答
代码逻辑是没有问题的,你在jsp页面中有没有导入jquery的包?因为没导入jquery的包是无法使用ajax的,你可以在浏览器中按F12进入控制台查看错误原因。祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧