ajax问题

来源:2-7 利用Ajax实现新闻列表

qq_a污骨_0

2019-03-05 14:39:23

有两个问题

1.html中date时间这一项不识别,如图

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

2.页面不显示数据,我用下面的代码检测了一下,是“1”,是哪里出了问题?

else if (xmlhttp.readyState == 4) {
        console.log("4");
    } else if (xmlhttp.readyState == 3) {
        console.log("3");
    } else if (xmlhttp.readyState == 2) {
        console.log("2");
    } else if (xmlhttp.readyState == 1) {
        console.log("1");
    } else if (xmlhttp.readyState == 0) {
        console.log("0");
    }




下面是所有的代码

NewListServlet.java

package 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(name = "NewListServlet",urlPatterns = "/news_list")
public class NewListServlet extends HttpServlet {
    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年5月份全球编程语言排行榜", "2018-5-1", "TIOBE", "..."));
        list.add(new News("TIOBE:2018年5月份全球编程语言排行榜", "2018-5-1", "TIOBE", "..."));
        list.add(new News("TIOBE:2018年5月份全球编程语言排行榜", "2018-5-1", "TIOBE", "..."));
        list.add(new News("TIOBE:2018年5月份全球编程语言排行榜", "2018-5-1", "TIOBE", "..."));
        String json = JSON.toJSONString(list);//将java对象序列化生成字符串
        System.out.println(json);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(json);
    }
}

News.java

package ajax;


public class News {
    private String title;
    private String date;
    private String source;
    private String content;

    public News() {
    }

    public News(String title, String date, String source, String content) {
        this.title = title;
        this.date = date;
        this.source = source;
        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 getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

news.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ajax</title>
</head>
<body>
<div id="container">
</div>
<script type="text/javascript">
 //1.创建XmlHttpRequest
 var xmlhttp;
 if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
 } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 }
    //2.发送Ajax请求
 xmlhttp.open("GET", "/ajax/news_list", true);
 xmlhttp.send();
 //3.处理服务器响应
 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var text = xmlhttp.responseText;
 console.log(text);
 var json = JSON.parse(text);
 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.date+ "&nbsp;" + news.source + "</h2>"
 html = html + "<hr/>"
 }
        document.getElementById("container").innerHTML = html;
 }
//    else if (xmlhttp.readyState == 4) {
//            console.log("4");
//        } else if (xmlhttp.readyState == 3) {
//            console.log("3");
//        } else if (xmlhttp.readyState == 2) {
//            console.log("2");
//        } else if (xmlhttp.readyState == 1) {
//            console.log("1");
//        } else if (xmlhttp.readyState == 0) {
//            console.log("0");
//        }

</script>
</body>
</html>


写回答

2回答

好帮手慕阿莹

2019-03-05

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

1、同学好,经测试,老师这里的date并没有报同学图中的提示,建议同学把代码重新粘一下试试。

2、这里改成false后可以正常显示了

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


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

改成false,表示使用同步。

如果使用异步,需要像老师用这个onreadystatechange处理:

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

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


0
hq_a污骨_0
h 老师,我用的idea,第二个问题已经完美解决了,第一个date问题的话,如果我去掉news.date,浏览器输出的内容就没有日期了,写上的话浏览器还是可以输出日期的,但是idea还是不识别它,其他的属性都没有问题,就date不识别,而且浏览器能显示date的数据
h019-03-05
共1条回复

qq_a污骨_0

提问者

2019-03-05

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

接https://class.imooc.com/course/qadetail/96625的问题,使用的是idea,从上图能看出来,就没有new.date这个选项

0

0 学习 · 10204 问题

查看课程