老师帮我看看该怎样修改

来源:2-8 自由编程

Wonwayshon

2020-10-12 16:03:23

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ajax</title>
<style type="text/css">
	input{
		width:200px;
		height:25px;
	}
</style>
</head>
<body>
	<div style="text-align:center;">
		<input type="button" value="员工列表" class="btn" name="0">
		<input type="button" value="职位列表" class="btn" name="1">
		<input type="button" value="部门列表" class="btn" name="2">
		<div id="div1"></div>
	</div>
	<script type="text/javascript">
		var btn=document.getElementByClassName("btn");
		for(var i=0;i<btn.length;i++){
			btn[i].onclick=function(){
				var xmlhttp,
					name=this.name;
				if(window.XMLHttpRequest){
					xmlhttp=new XMLHttpRequest();
				}else{
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				xmlhttp.open("get","/Ajax/Ajax?name="+name,true);
				xmlhttp.send();
				xmlhttp.onreadystatechange=function(){
					if(xmlhttp.readyState==4 && xmlhttp.status==200){
						var json=JSON.parse(xmlhttp.responseText);
							str="";
						for(int i=0;i<json.length;i++){
							str+=json[i]+"</br>";
						}
						document.getElementById("div1").innerHTML=str;
					}
				}
			}
		}
	</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 Ajax
 */
@WebServlet("/Ajax")
public class Ajax extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Ajax() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List el=new ArrayList();
		List pl=new ArrayList();
		List dl=new ArrayList();
		el.add("小红");
		el.add("小明");
		el.add("小白");
		pl.add("职员");
		pl.add("经理");
		dl.add("人事部");
		dl.add("技术部");
		dl.add("无线事业部");
		String els=JSON.toJSONString(el);
		String pls=JSON.toJSONString(pl);
		String dls=JSON.toJSONString(dl);
		String name=(String)(request.getParameter("name"));
		String responseString=null;
		if(name.equals("0")) {
			responseString=els;
		}else if(name.equals("1")) {
			responseString=pls;
		}else if(name.equals("2")) {
			responseString=dls;
		}
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().println(responseString);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}


写回答

1回答

好帮手慕小尤

2020-10-12

同学你好,1. getElementsByClassName少写了一个s,建议同学添加上。如下所示:

http://img.mukewang.com/climg/5f84190309c81f7b06810131.jpg

2. 在JavaScript中定义变量使用var即可,并不需要使用数据类型。如下所示:

http://img.mukewang.com/climg/5f8418d8093c548606860234.jpg

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

0

0 学习 · 9666 问题

查看课程