最后没有输出,别的都没问题,就最后一步
问题描述:
相关代码:
package com.imooc.ajax;
public class News {
private String title;
private String date;
private String sourse;
private String content;
public News() {
super();
}
public News(String title, String date, String sourse, String content) {
super();
this.title = title;
this.date = date;
this.sourse = sourse;
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getSourse() {
return sourse;
}
public void setSourse(String sourse) {
this.sourse = sourse;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
相关代码:
package com.imooc.ajax;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* Servlet implementation class NewsListServlet
*/
@WebServlet("/news_list")
public class NewsListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public NewsListServlet() {
// 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 News("TIOBE:2018年5月份全球编程语言排行榜","2018-5-1","TIOBE","..."));
list.add(new News("TIOBE:2018年6月份全球编程语言排行榜","2018-6-1","TIOBE","..."));
list.add(new News("TIOBE:2018年7月份全球编程语言排行榜","2018-7-1","TIOBE","..."));
list.add(new News("TIOBE:2018年8月份全球编程语言排行榜","2018-8-1","TIOBE","..."));
String json=JSON.toJSONString(list);
System.out.println(json);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().println(json);
}
}
相关代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<script>
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://localhost:8080/ajax/news_list","true");
xmlhttp.send();
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var text=xmlhttp.responseText;
alert(text);
var json=JSON.parse(text);
console.log(json);
var html="";
for(var i=0;i<json.length;i++){
var news=json[i];
//console.log(news);
html=html+"<h1>"+news.title+"</h1>";
html=html+"<h2>"+news.date+" "+news.sourse+"</h2>";
html=html+"<hr/>"
}
document.getElementById("container").innerHTML=html;
}
}
</script>
</div>
</body>
</html>
问题描述:
老师好,为什么和视频中同样的代码,这里报错了,要怎么解决啊
10
收起
正在回答 回答被采纳积分+1
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星