老师,这个为什么错了?

老师,这个为什么错了?

http://img1.sycdn.imooc.com//climg/5d8a28b709e4d7c603240021.jpg

这是代码:

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<div align="center">

<input type="button" id="employee" value="员工列表" style="width:300px">

<input type="button" id="position" value="职工列表"  style="width:300px">

<input type="button" id="department" value="部门列表"  style="width:300px">

</div>

<div align="center" id="container"></div>

<script type="text/javascript">

var xmlhttp;

if(window.XMLHttpRequest){

xml=new XMLHttpRequest();

}else{

xml=new ActiveXObject("Microsoft","XMLHTTP");

}

document.getElementById("employee").onclick=function(){

xmlhttp.open("GET","/ajax/employee?flag=1",true);

xmlhttp.send();

xmlhttp.onreadystatechange=function(){

if(xmlhttp.readyState==4 && xmlhttp.status==200){

var text=xmlhttp.responseText;

var json=JSON.parse(text);

var html="";

for(var i=0;i<json.length;i++){

var emp=json[i];

html=html+"<h3>"+emp+"</h3>";

}

document.getElementById("container").innerHTML=html;

}

}

}

</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 EmployeeServlet

 */

@WebServlet("/employee")

public class EmployeeServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public EmployeeServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

List list1=new ArrayList();

list1.add("小红");

list1.add("小明");

list1.add("小白");

List list2=new ArrayList();

list2.add("职员");

list2.add("经理");

List list3=new ArrayList();

list3.add("人事部");

list3.add("技术部");

list3.add("无线事业部");

String json1=JSON.toJSONString(list1);

String json2=JSON.toJSONString(list2);

String json3=JSON.toJSONString(list3);

response.setContentType("text/html;charset=UTF-8");

String num=request.getParameter("flag");

int num1=Integer.parseInt(num);

if(num1==1) {

response.getWriter().println(json1);

}else if(num1==2) {

response.getWriter().println(json2);

}else if(num1==3) {

response.getWriter().println(json3);

}

}

}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
提问者 精慕门618366 2019-09-24 22:52:28

已解决,找到问题所在了

  • 同学很棒呐,调错是编程中很大一部分内容,继续加油 祝:学习愉快~
    2019-09-25 09:41:32
提问者 精慕门618366 2019-09-24 22:34:49

这是错误信息:

Uncaught TypeError: Cannot read property 'open' of undefined

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师