response.getWriter().println(json);这一句为什么不会再输出了?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.imooc.ajax; import com.alibaba.fastjson.JSON; 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 java.io.IOException; import java.util.ArrayList; import java.util.List; @WebServlet (urlPatterns = "/news_list" ) public class NewsListServlet extends HttpServlet { public NewsListServlet() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List list = new ArrayList(); list.add( new News( "TIOBE:2018年5月份全球编程语言排行榜" , "2018-5-2" , "TIOBE" , "..." )); list.add( new News( "TIOBE:2018年6月份全球编程语言排行榜" , "2018-5-2" , "TIOBE" , "..." )); list.add( new News( "TIOBE:2018年7月份全球编程语言排行榜" , "2018-5-2" , "TIOBE" , "..." )); list.add( new News( "TIOBE:2018年8月份全球编程语言排行榜" , "2018-5-2" , "TIOBE" , "..." )); list.add( new News( "TIOBE:2018年9月份全球编程语言排行榜" , "2018-5-2" , "TIOBE" , "..." )); list.add( new News( "TIOBE:2018年10月份全球编程语言排行榜" , "2018-5-2" , "TIOBE" , "..." )); String json = JSON.toJSONString(list); System.out.println(json); response.setContentType( "text/html;charset=utf-8" ); response.getWriter().println(json); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < title >Title</ title ></ head > < body > < div id = "container" ></ div > < script type = "text/javascript" > var xml = new XMLHttpRequest(); xml.open("GET", "/ajax/news_list", true); xml.send(); xml.onreadystatechange = function () { if (xml.status == 200 && xml.readyState == 4) { var xmlText = xml.responseText; console.log(xmlText); var json = JSON.parse(xmlText); console.log(json); var html = ""; for (var i = 0; i < json.length; i++) { var news = json[i]; html = html + "< h1 >" + news.title + "</ h1 >"; html = html + "< h2 >" + news.data + " " + news.source + "</ h2 >"; html = html + "< hr />"; } document.getElementById("container").innerHTML = html; } }; </ script > </ body > </ html > |
response.getWriter().println(json);
这一句为什么不会再输出了?
14
收起
正在回答 回答被采纳积分+1
2回答
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧