课题打卡~请老师检查
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div style="width: 620px;"> <form action="ajax/emp" method="get"> <input style="width: 200px;" type="button" id="popular" name="popular" value="流行歌曲"> <input style="width: 200px;" type="button" id="classic" name="classic" value="经典歌曲"> <input style="width: 200px;" type="button" id="rock" name="rock" value="摇滚歌曲"> </form> </div> <div style="width: 600px; text-align: center;"> <div id="content"></div> </div> <script type="text/javascript" src="js/jquery-3.3.1.js"></script> <script type="text/javascript"> $(function() { $("#popular").click(function() { initAjax("ajax/song","get","flag=popular","json"); }); $("#classic").click(function() { initAjax("ajax/song","get","flag=classic","json"); }); $("#rock").click(function() { initAjax("ajax/song","get","flag=rock","json"); }); }); function initAjax(url,type,data,dataType){ $.ajax({ "url" : url, "type" : type, "data" : data, "dataType" : dataType, "success" : function(json) { $("#content").text(""); console.log(json.length); for(var i=0;i<json.length;i++){ $("#content").append("<h2><b>"+json[i].name+"</b></h2>"); } }, "error" : function(xmlhttp, errorText) { console.log(xmlhttp); console.log(errorText); if(xmlhttp.status == 400){ alert("无效的请求"); }else if(xmlhttp.status == 404){ alert("无法找到文件"); }else if(xmlhttp.status == 500){ alert("内部服务器错误"); } } }); } </script> </body> </html>
package com.imooc.ajax; 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("/ajax/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 { List<Song> song = new ArrayList<Song>(); String flag = request.getParameter("flag"); if (flag.equals("popular")) { song.add(new Song(1, "稻香")); song.add(new Song(2, "晴天")); song.add(new Song(3, "告白气球")); } else if (flag.equals("classic")) { song.add(new Song(1, "千千阙歌")); song.add(new Song(2, "傻女")); song.add(new Song(3, "七友")); } else if (flag.equals("rock")) { song.add(new Song(1, "一块红布")); song.add(new Song(2, "假行憎")); song.add(new Song(3, "新长征路上的摇滚")); } String json = JSON.toJSONString(song); response.setContentType("text/html;charset=UTF-8"); response.getWriter().println(json); } }
25
收起
正在回答 回答被采纳积分+1
1回答
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星