ajax查询问题

ajax查询问题

后台没有报错,但每次输完查询内容点击查询按钮,页面只刷新没有筛选查询内容,还是原来的数据,麻烦老师看看是哪里出了问题

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>图书后台管理</title>

<link rel="stylesheet" href="css/index.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

<script type="text/javascript" src="js/jquery-3.4.1.js"></script>

</head>


<body>

<header>

<div class="container">

<nav>

<a href="bookList.jsp">图书信息管理</a>

</nav>

<nav>

<a href="categoryList.jsp">分类管理</a>

</nav>


</div>

</header>

<section class="banner">

<div class="container">

<div>

<h1>图书管理系统</h1>

<p>图书信息管理</p>

</div>

</div>

</section>

<section class="main">



<div class="container">

<form class="form-horizontal" action="${pageContext.request.contextPath}/SearchBookServlet" method="post">

<div class="form-group" style="float: right;">

<div class="col-sm-offset-2 col-sm-10">

<button type="submit" class="btn btn-primary">查询</button>

&nbsp;&nbsp;&nbsp;

</div>

</div>

<div class="form-group" style="float: right; width: 300px;">

<div class="col-sm-8">

<input name="searchContent" class="form-control"

id="searchContent" placeholder="输入要查询的分类" style="width: 250px">

</div>

</div>



</form>

</div>

<div class="container">


<table class="table table-striped">

<thead>

<tr>

<th>序号</th>

<th>图书编号</th>

<th>图书名称</th>

<th>分类</th>

<th>价格</th>

<th>图书封面</th>

<th>操作</th>


</tr>

</thead>

<tbody id="tbody">

<c:forEach items="${sessionScope.bookList }" var="bl"

varStatus="idx">

<tr id="tr1">

<td>${idx.index+1}</td>

<td>${bl.bookId}</td>

<td>${bl.bookName}</td>

<td>${bl.categoryName}</td>

<td>¥${bl.bookPrice}</td>

<td><img src="${bl.bookPicture}"></td>

<td><a

href="${pageContext.request.contextPath}/updateBook.jsp?bookId=${bl.bookId}">修改</a>

<a

href="${pageContext.request.contextPath}/DeleteBookServlet?bookId=${bl.bookId}">删除</a>


</td>


</tr>

</c:forEach>

</tbody>

</table>

</div>

</section>

<section class="page">

<div class="container">

<div id="fatie">

<a href="addBook.jsp"><button>新建</button></a>

</div>

</div>

</section>

<footer> copy@慕课网 </footer>

</body>

<!-- js的代码编写在</body>结束标签的下面,

    因为页面的加载顺序是从上到下的顺序进行加载的,

    避免 js 代码操作 HTML 元素时,HTML 元素还未载入而失效 

    -->

<script type="text/javascript">

$("selector.submit").click(

function() {

$.ajax({

"url" : "/bookSystem/SearchBookServlet",

"type" : "get",

"data" : {

"searchContent" : $("input[name='searchContent']").val()

},

"dataType" : "json",

"success" : function(json) {

$("#tbody").empty();//将tbody的元素内容清空

if (json.length == 0) {

return;

alert("没有数据");

}

console.log(json);

for (var n = 0; n < json.length; n++) {

$("tbody").append(

"<tr id=\"tr1\">" +

"<td>" + (n + 1) + "</td>" +

"<td>" + json[n].bookId + "</td>" +

"<td>" + json[n].bookName + "</td>"+

"<td>"+ json[n].categoryName+ "</td>" +

"<td>" + json[n].bookPrice+ "</td>" +

//ajax代码中获取json中的图片属性,要添加.cover

"<td><img src="+json[n].cover  +"></td>"+

"<td><a href=\"updateBook.jsp?bookId="+ json[n].bookId+ "\">修改</a>" +

"<a href=\"DeleteBookServlet?bookId="+ json[n].bookId+ "\">删除</a></td>")

}

}

})

});

</script>

</html>

package com.imooc.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 com.alibaba.fastjson.JSON;

import com.imooc.domain.Book;

import com.imooc.service.impl.BookServiceImpl;


@WebServlet("/SearchBookServlet")

public class SearchBookServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String searchContent=request.getParameter("searchContent");

List<Book> bookList=(List<Book>)request.getSession().getAttribute("bookList");

BookServiceImpl bsi=new BookServiceImpl();

List<Book>searchlist=bsi.getBookByCondition(searchContent);

String json=JSON.toJSONString(searchlist);

response.setContentType("text/html;charset=utf-8");

response.getWriter().println(json);

response.sendRedirect(request.getContextPath()+"/bookList.jsp");

// request.getRequestDispatcher("/bookList.jsp").forward(request, response);


}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}


}

package com.imooc.service.impl;


import java.util.ArrayList;

import java.util.List;


import com.imooc.domain.Book;


//图书信息处理类

public class BookServiceImpl {

// 描述图书信息:

private static final List<Book> myBook = new ArrayList<Book>();


// 获取所有图书

public List<Book> getBooks() {

return myBook;

}


// 添加图书信息:

public void addBook(Book book) {

myBook.add(book);

}


// 修改图书信息

public void updateBook(Book book) {

deleteBook(book.getBookId());

myBook.add(book);

}


// 删除图书信息

public void deleteBook(String bookId) {

for (Book book : myBook) {

if (book.getBookId().equals(bookId)) {

myBook.remove(book);

break;

}

}

}


// 根据图书ID来查询书籍信息:

public Book getBookIdByCondtion(String bookId) {

for (Book books : myBook) {

if (books.getBookId().equals(bookId)) {

return books;

}

}

return null;

}


// 根据分类来查询书籍信息:

public List<Book> getBookByCondition(String categoryName) {

List<Book> bookList = new ArrayList<Book>();

for (Book books : myBook) {

if (books.getCategoryName().equals(categoryName)) {

bookList.add(books);

}

}

return bookList;

}


}


正在回答

登陆购买课程后可参与讨论,去登陆

5回答

同学你好,

1、ajax传入的参数名为searchWord,则在获取参数是也应该是searchWord,具体如下:

http://img1.sycdn.imooc.com//climg/5e5b838709f435a706460138.jpg

http://img1.sycdn.imooc.com//climg/5e5b83fd09adbde807770166.jpg

2、获取值的$编写错了,具体如下:

http://img1.sycdn.imooc.com//climg/5e5b844e09596c2b05080228.jpg

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

提问者 niuhuiyang 2020-03-01 15:04:33

老师,我修改了click,但点击查询还是没有反应,以下是详细代码,麻烦老师看看出了什么问题

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>图书后台管理</title>

<link rel="stylesheet" href="css/index.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

<script type="text/javascript" src="js/jquery-3.4.1.js"></script>

</head>


<body>

<header>

<div class="container">

<nav>

<a href="bookList.jsp">图书信息管理</a>

</nav>

<nav>

<a href="categoryList.jsp">分类管理</a>

</nav>


</div>

</header>

<section class="banner">

<div class="container">

<div>

<h1>图书管理系统</h1>

<p>图书信息管理</p>

</div>

</div>

</section>

<section class="main">



<div class="container">

<form id="search" class="form-horizontal" action="${pageContext.request.contextPath}/SearchBookServlet" method="post">

<div class="form-group" style="float: right;">

<div class="col-sm-offset-2 col-sm-10">

<!-- 类型submit会在提交后自动刷新页面,所以对应ajax查询到的数据会在刷新后消失 -->

<button type="button" id="find" class="btn btn-primary">查询</button>

&nbsp;&nbsp;&nbsp;

</div>

</div>

<div class="form-group" style="float: right; width: 300px;">

<div class="col-sm-8">

<input name="searchContent" class="form-control"

id="searchContent" placeholder="输入要查询的分类" style="width: 250px">

</div>

</div>



</form>

</div>

<div class="container">


<table class="table table-striped">

<thead>

<tr>

<th>序号</th>

<th>图书编号</th>

<th>图书名称</th>

<th>分类</th>

<th>价格</th>

<th>图书封面</th>

<th>操作</th>


</tr>

</thead>

<tbody id="tb">

<c:forEach items="${sessionScope.bookList }" var="bl"

varStatus="idx">

<tr id="tr1">

<td>${idx.index+1}</td>

<td>${bl.bookId}</td>

<td>${bl.bookName}</td>

<td>${bl.categoryName}</td>

<td>¥${bl.bookPrice}</td>

<td><img src="${bl.bookPicture}"></td>

<td><a

href="${pageContext.request.contextPath}/UpdateJumpServlet?bookId=${bl.bookId}">修改</a>

<a

href="${pageContext.request.contextPath}/DeleteBookServlet?bookId=${bl.bookId}">删除</a>


</td>


</tr>

</c:forEach>

</tbody>

</table>

</div>

</section>

<section class="page">

<div class="container">

<div id="fatie">

<a href="addBook.jsp"><button>新建</button></a>

</div>

</div>

</section>

<footer> copy@慕课网 </footer>

</body>

<!-- js的代码编写在</body>结束标签的下面,

    因为页面的加载顺序是从上到下的顺序进行加载的,

    避免 js 代码操作 HTML 元素时,HTML 元素还未载入而失效 

    -->

<script type="text/javascript">

$("#find").click(function(){

$.ajax({

  "url":"${pageContext.request.contextPath}/SearchBookServlet",

  "type":"post",

  "data":{"searchWord":$("#searchContent").val()},

  "dataType":"json",

  "success":function(json){

  var content="";

  for(var i=0;i<json.length;i++){

  content=content+"<tr><td>"+(i+1)+"</td><td>"+json[i].bookId+"</td><td>"+json[i].bookName+"</td><td>"+json[i].categoryName+"</td><td>"+json[i].bookPrice+"</td><td> <a href='${pageContext.request.contextPath}/updateBook.jsp?bookId="+json[i].bookId+"'>修改</a><a href='${pageContext.request.contextPath}/DeleteBookServlet?bookId="+json[i].bookId+"'>删除</a></td></tr>";

  $("#tb>tr").remove();

  }

  $("#tb").html(content);

  }

  });

})

 

</script>

</html>

package com.imooc.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 com.alibaba.fastjson.JSON;

import com.imooc.domain.Book;

import com.imooc.service.impl.BookServiceImpl;


@WebServlet("/SearchBookServlet")

public class SearchBookServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String searchContent=request.getParameter("searchContent");

List<Book> bookList=(List<Book>)request.getSession().getAttribute("bookList");

BookServiceImpl bsi=new BookServiceImpl();

List<Book>searchlist=bsi.getBookByCondition(searchContent);

String json=JSON.toJSONString(searchlist);

response.setContentType("text/html;charset=utf-8");

response.getWriter().println(json);

response.sendRedirect(request.getContextPath()+"/bookList.jsp");

// request.getRequestDispatcher("/bookList.jsp").forward(request, response);


}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}


}


好帮手慕酷酷 2020-03-01 11:59:09

同学你好,jQuery中没有onclick事件。要使用click事件,表示点击find控件会执行function(){};,具体如下:

http://img1.sycdn.imooc.com//climg/5e5b326d09dbf53b05100115.jpg

由于没有完整的代码老师无法进行整体的调试,如果同学还是没有解决,建议同学上交作业,老师进行整体的调试,反馈给同学。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

提问者 niuhuiyang 2020-02-29 20:53:32

老师,这是用了button后的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>图书后台管理</title>

<link rel="stylesheet" href="css/index.css">

<link rel="stylesheet" href="css/bootstrap.min.css">

<script type="text/javascript" src="js/jquery-3.4.1.js"></script>

</head>


<body>

<header>

<div class="container">

<nav>

<a href="bookList.jsp">图书信息管理</a>

</nav>

<nav>

<a href="categoryList.jsp">分类管理</a>

</nav>


</div>

</header>

<section class="banner">

<div class="container">

<div>

<h1>图书管理系统</h1>

<p>图书信息管理</p>

</div>

</div>

</section>

<section class="main">



<div class="container">

<form id="search" class="form-horizontal" action="${pageContext.request.contextPath}/SearchBookServlet" method="post">

<div class="form-group" style="float: right;">

<div class="col-sm-offset-2 col-sm-10">

<!-- 类型submit会在提交后自动刷新页面,所以对应ajax查询到的数据会在刷新后消失 -->

<button type="button" id="find" class="btn btn-primary">查询</button>

&nbsp;&nbsp;&nbsp;

</div>

</div>

<div class="form-group" style="float: right; width: 300px;">

<div class="col-sm-8">

<input name="searchContent" class="form-control"

id="searchContent" placeholder="输入要查询的分类" style="width: 250px">

</div>

</div>



</form>

</div>

<div class="container">


<table class="table table-striped">

<thead>

<tr>

<th>序号</th>

<th>图书编号</th>

<th>图书名称</th>

<th>分类</th>

<th>价格</th>

<th>图书封面</th>

<th>操作</th>


</tr>

</thead>

<tbody id="tb">

<c:forEach items="${sessionScope.bookList }" var="bl"

varStatus="idx">

<tr id="tr1">

<td>${idx.index+1}</td>

<td>${bl.bookId}</td>

<td>${bl.bookName}</td>

<td>${bl.categoryName}</td>

<td>¥${bl.bookPrice}</td>

<td><img src="${bl.bookPicture}"></td>

<td><a

href="${pageContext.request.contextPath}/updateBook.jsp?bookId=${bl.bookId}">修改</a>

<a

href="${pageContext.request.contextPath}/DeleteBookServlet?bookId=${bl.bookId}">删除</a>


</td>


</tr>

</c:forEach>

</tbody>

</table>

</div>

</section>

<section class="page">

<div class="container">

<div id="fatie">

<a href="addBook.jsp"><button>新建</button></a>

</div>

</div>

</section>

<footer> copy@慕课网 </footer>

</body>

<!-- js的代码编写在</body>结束标签的下面,

    因为页面的加载顺序是从上到下的顺序进行加载的,

    避免 js 代码操作 HTML 元素时,HTML 元素还未载入而失效 

    -->

<script type="text/javascript">

$("#find").onclick=function(){

$.ajax({

  "url":"${pageContext.request.contextPath}/SearchBookServlet",

  "type":"post",

  "data":{"searchWord":$("#searchContent").val()},

  "dataType":"json",

  "success":function(json){

  var content="";

  for(var i=0;i<json.length;i++){

  content=content+"<tr><td>"+(i+1)+"</td><td>"+json[i].bookId+"</td><td>"+json[i].bookName+"</td><td>"+json[i].categoryName+"</td><td>"+json[i].bookPrice+"</td><td> <a href='${pageContext.request.contextPath}/updateBook.jsp?bookId="+json[i].bookId+"'>修改</a><a href='${pageContext.request.contextPath}/DeleteBookServlet?bookId="+json[i].bookId+"'>删除</a></td></tr>";

  $("#tb>tr").remove();

  }

  $("#tb").html(content);

  }

  });

}

</script>

</html>

但点击查询还是什么反应都没有,麻烦老师看下是什么问题,谢谢老师

好帮手慕小班 2020-02-29 19:27:47

同学你好,查看同学贴出代码,在查询按钮中使用了submit

http://img1.sycdn.imooc.com/climg/5e5a49e70939674005330095.jpg

类型是submit,submit会在提交后自动刷新页面,所以对应ajax查询到的数据会在刷新后消失,如上所述,同学将submit改为button按钮再来试一下。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

  • 提问者 niuhuiyang #1
    老师,我试了用button,点击查询页面没有任何反应
    2020-02-29 20:38:26
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师