最后没有输出,别的都没问题,就最后一步

最后没有输出,别的都没问题,就最后一步

问题描述:

http://img1.sycdn.imooc.com//climg/609b688109ea668c14040838.jpg

相关代码:

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+"&nbsp;"+news.sourse+"</h2>";
html=html+"<hr/>"
}

document.getElementById("container").innerHTML=html;
}
}
</script>
</div>
</body>
</html>

问题描述:

老师好,为什么和视频中同样的代码,这里报错了,要怎么解决啊

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

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

1回答
____xyz 提问者 2021-05-12 15:08:03

老师好,我自己找出问题了,div的ID没有写,暂时不用麻烦老师了哈

  • 同学很棒,已经自己解决问题了,在开发中自己调整问题也是很大一部分内容,继续加油。

    祝学习愉快!

    2021-05-12 15:31:39
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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