简体   繁体   English

在JavaScript中使用XMLlHttpRequest检索服务器端数据,并在新创建的li元素中填充项目

[英]Retrieve server side data using XMLlHttpRequest in javascript and populate the items in newly created li element

I am kinda new to javascript and trying to learn some basic operation. 我对javascript有点陌生,并尝试学习一些基本操作。 I have a dynamically created li element where a list of notes are being displayed. 我有一个动态创建的li元素,其中正在显示注释列表。

Adding notes to the server is working well. 向服务器添加注释工作正常。 Each note has an id and name value. 每个注释都有一个ID和名称值。 The jason object is stored in this form [{"id":"1","name":"This is the first note"}] . jason对象以[{"id":"1","name":"This is the first note"}]

Now I want to retrieve the list from the server and display only the "name" values as the list on the same page whenever the user leaves the page and open the page again. 现在,我想从服务器检索列表,并且每当用户离开该页面并再次打开该页面时,在同一页面上仅将“名称”值显示为列表。

The controller function looks like this: 控制器功能如下所示:

public class NoteController {

    private List<Note> notes;

    public NoteController() {
        this.notes = new ArrayList<>();
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<Note> noteList() {
        return this.notes;
    }
}

Heres the html code: 这是html代码:

       <form>
            <input type="text" name="name" id="name"/>
            <input type="button" onclick="addNote();" value="Add"/>
        </form>

And here is my javascript code: 这是我的JavaScript代码:

        function loadNotes() {

            var HttpGET = new XMLHttpRequest();
            HttpGET.open("GET", "url", true);
            HttpGET.onreadystatechange = function() {

                if (HttpGET.readyState === 4) {
                    if (HttpGET.status === 200) {

                    var response = JSON.parse(HttpGET.responseText);
                    var loadListElement = document.createElement("li");

                 loadListElement.appendChild(document.createTextNode("name"));
                                                         document.querySelector("#notes").appendChild(loadListElement);
                    document.querySelector("#notes").innerHTML = response.value.notes;
                    }
                }
            }

            HttpGET.send(null);
        }

The javascript code is not working. javascript代码无法正常工作。 The Json data should be displayed inside the ul element within its child node li. Json数据应显示在其子节点li内的ul元素内。 But I see nothing when I close the page or reload. 但是关闭页面或重新加载时什么也看不到。 I think I am not sure if I have used the response value properly in the loadTasks function. 我想我不确定在loadTasks函数中是否正确使用了响应值。 Also I am note sure if I have created text node properly in document.createTextNode("name"). 另外,我还要注意是否在document.createTextNode(“ name”)中正确创建了文本节点。 Or am I missing something? 还是我错过了什么? Please help me to get through this. 请帮助我解决这个问题。 I spent quite much time on this already and cant think anything else other than banging my head here in stackoverflow. 我已经花了很多时间在此上,除了在stackoverflow中敲打我的头之外,别无选择。

As per your comments I assume you are getting response in the following format 根据您的评论,我想您会以以下格式获得答复

    [
      {"id":"9d0962e8-e1f5-4b61-85ed-489dbc6d5bb2","name":"Runnin‌​g"},
      {"id":"82e1ad1a-‌​9bfb-490f-8f95-90139‌​dfe6790","name":"Sle‌​eping"}
    ]

Now your code should look like this 现在您的代码应如下所示

xmlHttp.onreadystatechange = function() {    
    if (xmlHttp.readyState === 4) {
          if (xmlHttp.status === 200) { 
              if(xmlHttp.responseText){// check for response
                 var response = JSON.parse(xmlHttp.responseText);
                 for(var note in response){ //iterate over response object
                     if(response.hasOwnProperty(note)){ //check whether note is a property of response object
                        var loadListElement = document.createElement("li");
                        loadListElement.appendChild(document.createTextNode(response‌​[note].name)); //use note's name property to get actual text
                        document.querySelector("#notes").appendChild(loadListElement);//append the element
                     }
                 }

              }
          }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM