请问老师检查一下,为什么点会出现404呢?
来源:3-4 Ajax函数的使用
KelvinChung
2019-05-22 21:15:22
package com.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 MusicListServlet */ @WebServlet("/MusicListServlet") public class MusicListServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public MusicListServlet() { super(); // TODO Auto-generated constructor stub } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //String type = request.getParameter("t"); List<String> list1 = new ArrayList<String>(); list1.add("稻香"); list1.add("晴天"); list1.add("告白气球"); List<String> list2 = new ArrayList<String>(); list2.add("千千阙歌"); list2.add("傻女"); list2.add("七友"); List<String> list3 = new ArrayList<String>(); list3.add("一块红布"); list3.add("假行僧"); list3.add("新长征路上的摇滚"); String list11 = JSON.toJSONString(list1); String list22 = JSON.toJSONString(list2); String list33 = JSON.toJSONString(list3); response.setContentType("text/html;charset=utf-8"); if(request.getParameter("id").equals("a")) { System.out.println(list11); response.getWriter().println(list11); } else if(request.getParameter("id").equals("b")) { System.out.println(list22); response.getWriter().println(list22); } else if(request.getParameter("id").equals("c")) { System.out.println(list33); response.getWriter().println(list33); } } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <table align = "center"> <tr> <td><input style ="width:200px" type = "button" id = "a" value = "流行歌曲"></li> <td><input style ="width:200px" type = "button" id = "b" value = "经典歌曲"></li> <td><input style ="width:200px" type = "button" id = "c" value = "摇滚歌曲"></li> </tr> </table> <div id = "d" align = "center"></div> <script type = "text/javascript" src = "js/jquery-3.4.1.js"></script> <script type = "text/javascript"> $("#a,#b,#c").click(function(){ var id = this.id; var url = "/ajax/MusicListServlet?id=" + id; var a = "id="+id; $.ajax({ "url" : url, "type" : "post", "data" : a, "dataType" : "json", "success": function(json){ console.log(json); var html=""; for(var i=0;i<json.length;i++){ html = html + "<div>" + json[i] + "</div>"; } document.getElementById("d").innerHTML = html; }, "error":function(xmlhttp,errorText){ //console.log(xmlhttp); if(xmlhttp.status==405){ alert("无效的请求"); }else if(xmlhttp.status==404){ alert("未找到URL资源"); }else if(xmlhttp.status==500){ alert("服务器内部错误,请联系管理员"); }else{ alert("错误,请联系管理员"); } } }) }) </script> </body> </html>
1回答
同学你好!同学的代码在老师这里可以正常运行呢,建议检查一下项目名称是否是ajax
重新启动项目测试一下。如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题
回答 4
回答 2