课题打卡~请老师检查

来源:2-8 自由编程

慕仙4530950

2020-07-09 15:09:53

<!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="emp" name="emp"
				value="员工列表"> <input style="width: 200px;" type="button"
				id="post" name="post" value="职位列表"> <input
				style="width: 200px;" type="button" id="depar" name="depar"
				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">
		$("#emp").click(function() {
			initAjax("GET", "ajax/emp?flag=emp", true);
		});
		$("#post").click(function() {
			initAjax("GET", "ajax/emp?flag=post", true);
		});
		$("#depar").click(function() {
			initAjax("GET", "ajax/emp?flag=depar", true);
		});
		/*给ajax赋值,传递三个参数:表单提交方式,路径,是否异步提交*/
		function initAjax(obj, url, flag) {
			var xmlhttp;
			if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest();
			} else {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.open(obj, url, flag);
			xmlhttp.send();
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					var content = xmlhttp.responseText;
					var json = JSON.parse(content);
					console.log(json);
					var html = "";
					for (var i = 0; i < json.length; i++) {
						html = html + "<div>" + json[i] + "</div>";
					}
					$("#content").html(html);
				}
			}
		}
	</script>
</body>
</html>
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<String> json = new ArrayList<String>();				
		String flag = request.getParameter("flag");
		if(flag.equals("emp")) {	
			json.add("小花");
			json.add("小明");
			json.add("小白");
		}else if(flag.equals("post")) {
			json.add("职员");
			json.add("经理");
		}else if(flag.equals("depar")) {
			json.add("人事部");
			json.add("技术部");
			json.add("无线事业部");
		}	
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().println(JSON.toJSONString(json));
	}
写回答

1回答

好帮手慕小脸

2020-07-09

同学你好,已完成练习,棒棒哒!继续加油!

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

1

0 学习 · 16556 问题

查看课程