无法显示名字请检查

来源:3-6 自由编程

warren_au

2020-06-11 10:20:23

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Insert title here</title>

</head>

<body>

<input type="button" id="btn1" value="员工列表">

<input type="button" id="btn2" value="职位列表">

<input type="button" id="btn3" value="部门列表">

<div id="divContext"></div>

<script type="text/javascript" src="js/jquery-3.5.1.js"></script>

<script type="text/javascript">

$("#btn1").click(function(){

$.ajax({

"url":"/ajax/prictice1",

"type":"get",

"data":"t",

"dataType":"json",

"success":function(json){

var html="";

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

var emp=json[i];

html=html+emp.name;

}

$("divContext").append(html);

}

})

})

</script>

</body>

</html>

Servlet

package wjb.prictice1;


import java.io.IOException;

import java.util.HashSet;

import java.util.Set;


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 Prictice1Servlet

 */

@WebServlet("/prictice1")

public class Prictice1Servlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Prictice1Servlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

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

*/

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

Set set=new HashSet();

set.add(new Emploee("小红","职员","外勤"));

set.add(new Emploee("小明","经理","整理"));

set.add(new Emploee("小白","CEO","批文"));

String json=JSON.toJSONString(set);

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

response.getWriter().println(json);

}


}


写回答

1回答

好帮手慕小尤

2020-06-11

同学你好,并没有divContext标签,则建议同学使用id选择器,获取到id为divContext的标签。修改后代码如下所示:

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

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

0

0 学习 · 16556 问题

查看课程