老师看看,点击按钮无响应
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 | package com.ajax.practice; public class Employee { private String name; private String job; private String department; public Employee() { } public Employee(String name, String job, String department) { super (); this .name = name; this .job = job; this .department = department; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getJob() { return job; } public void setJob(String job) { this .job = job; } public String getDepartment() { return department; } public void setDepartment(String department) { this .department = department; } } |
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 | package com.ajax.practice; 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 ajaxEmp */ @WebServlet ( "/ajaxEmp" ) public class ajaxEmp extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List list= new ArrayList(); list.add( new Employee( "小红" , "职员" , "人事部" )); list.add( new Employee( "小明" , "经理" , "技术部" )); list.add( new Employee( "小白" , "职员" , "无线事业部" )); String json=JSON.toJSONString(list); response.setContentType( "text/html;charset=UTF-8" ); response.getWriter().println(json); } } |
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 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >Insert title here</ title > </ head > < body > < div align = "center" > < input style = "width:200px" id = "btn0" type = "button" value = "员工列表" > < input style = "width:200px" id = "btn1" type = "button" value = "职位列表" > < input style = "width:200px" id = "btn2" type = "button" value = "部门列表" > </ div > < div id = "content" align = "center" ></ div > < script type = "text/javascript" > var xmmlhttp; if(windows.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); }else{ xmlhttp=new ActiveXOject("Microsoft.XMLHttp") } xmlhttp.open("GET","/jQueryPractice/ajaxEmp",true); xmlhttp.send(); xmlhttp.onreadyStatechange=function(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ var text=xmlhttp.responseText; var json=JSON.parse(text); //员工列表 document.getElementById("btn0").onclick=function(){ var html=""; var num=0; for(var i=0;i< json.length ;i++){ html+=json[i].name+"<br>"; } document.getElementById("content").innerHTML=html; } //职位列表 document.getElementById("btn1").onclick=function(){ var html=""; var num=0; for(var i=0;i< json.length ;i++){ var job = json [i].job; html+=json[i].job+"<br>"; } document.getElementById("content").innerHTML=html; //部门列表 document.getElementById("btn2").onclick=function(){ var html=""; for(var i=0;i< json.length ;i++){ html+=json[i].department+"<br>"; } document.getElementById("content").innerHTML=html; } } } } </ script > </ body > </ html > |
1
收起
正在回答
1回答
同学你好!
1.在你的代码中有个小问题,这里应该是window,同学多加了s
2.点击按钮没有反应是因为同学在点击按钮后没有去发送ajax请求,同学可以参考如下代码(以员工列表为例):
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题
登录后可查看更多问答,登录/注册
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧