点击按钮怎么没反应
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 | package com.imooc.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 com.alibaba.fastjson.JSON; /** * Servlet implementation class songservlet */ @WebServlet ( "/song" ) public class songservlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public songservlet() { super (); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String type = request.getParameter( "t" ); List<String> list = new ArrayList<>(); if (type.equals( "popular" )) { list.add( "稻香" ); list.add( "晴天" ); list.add( "告白气球" ); } else if (type.equals( "classic" )) { list.add( "千千阙歌" ); list.add( "傻女" ); list.add( "七友" ); } else if (type.equals( "rock" )) { list.add( "一块红布" ); list.add( "家行政" ); list.add( "新长征路上的摇滚" ); } String json = JSON.toJSONString(list); System.out.println(json); response.setContentType( "text/html;charset=utf-8" ); response.getWriter().println(json); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } |
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 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >Insert title here</ title > < script type = "text/javascript" src = "js/jquery-3.4.0.js" ></ script > < script type = "text/javascript" > $(function()){ $(".popular").click(function(){ $.ajax({ "url":"/FirstServlet/song", "type":"get", "data":"t=popular", "dataType":"json", "success":function(json) { console.log(json); for(var i =0;i< json.length ;i++){ $("#song").append("<h1>"+json[i]+"< h1 >"); } } }) }) } </ script > </ head > < body > < input type = "button" value = "流行歌曲" id = "popular" class = "popular" > < input type = "button" value = "经典歌曲" id = "classic" class = "classic" > < input type = "button" value = "摇滚歌曲" id= "rock" class = "rock" > < div id = "song" ></ div > </ body > </ html > |
2
收起
正在回答 回答被采纳积分+1
2回答
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧