请老师帮我检查一下2-8自由编程题目
来源:2-8 自由编程
宋舒
2020-06-19 11:03:48
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
input{
width:33%;
}
div{
text-align:center;
}
</style>
</head>
<body>
<input id="employee" type="button" value="员工列表">
<input id="job" type="button" value="职位列表">
<input id="department" type="button" value="部门列表">
<div id="content"></div>
<script type="text/javascript">
document.getElementById("employee").onclick=function(){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/ajaxwork/cs",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
var text=xmlhttp.responsetText;
var json=JSON.parse(text);
var html="";
for(var i=0;i<json.length;i++){
var employee= json[i];
html=html+"<h1>"+employee.name+"</h1>";
}
document.getElementById("content").innerHTML=html;
}
}
}
document.getElementById("job").onclick=function(){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/ajaxwork/cs",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
var text=xmlhttp.responsetText;
var json=JSON.parse(text);
var html="";
for(var i=0;i<json.length;i++){
var employee= json[i];
html=html+"<h1>"+employee.job+"</h1>";
}
document.getElementById("content").innerHTML=html;
}
}
}
document.getElementById("department").onclick=function(){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/ajaxwork/cs",true);
xmlhttp.send();
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
var text=xmlhttp.responsetText;
var json=JSON.parse(text);
var html="";
for(var i=0;i<json.length;i++){
var employee= json[i];
html=html+"<h1>"+employee.department+"</h1>";
}
document.getElementById("content").innerHTML=html;
}
}
}
</script>
</body>
</html>
package com.imooc.company;
public class Employee {
private String name;
private String job;
private String department;
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;
}
}
package com.imooc.company;
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 CompanyServlet
*/
@WebServlet("/cs")
public class CompanyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CompanyServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @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);
System.out.println(json);
response.setContentType("text/html;charset=utf-8");
response.getWriter().println(json);
}
}
以上是我写的代码,请老师检查下,一直得不到结果,为什么控制台一直提示收不到响应回来的JSON
1回答
同学你好,同学responseText书写错误,同学多了一个t,则建议同学修改为responseText。修改后代码如下所示:

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