简体   繁体   English

使用 jQuery/JavaScript 在 HTML 页面中显示/打印一组嵌套的 JSON API 数据(列表)

[英]Display/Print an Array of Nested JSON API data (list) in HTML page using jQuery/JavaScript

I have just started working on the jQuery,javaScript and AJAX and trying to consume an API designed by me.我刚刚开始研究 jQuery、javaScript 和 AJAX,并尝试使用我设计的 API。 The result at the backend is as follows后台结果如下

[
    {
        "id": 1,
        "choice_text": "They work on List View generally",
        "poll": 1
    },
    {
        "id": 3,
        "choice_text": "Learn on Digital Ocean",
        "poll": 2
    }
]

This is a list of Choices with id,poll id and Choice Text.这是一个带有 id、poll id 和选择文本的选项列表。 I want to display this data in the HTML page using the AJAX/javaScript/jQuery.我想使用 AJAX/javaScript/jQuery 在 HTML 页面中显示这些数据。 Any would do.任何人都可以。 This can Either be a table , list or div element.这可以是tablelistdiv元素。 How can I do this??我怎样才能做到这一点??

When I used the below code,当我使用下面的代码时,

$('#get-list').click(function () {
        $.get("http://127.0.0.1:8000/rest-api/choices/", function (data,status) {
            console.log(data);
            $.each(data, function () {
               $('div').html('<p>'+data+'</p>')
            })
            })
        })

I got我有

[object Object],[object Object]

in my page but the console.log() shows the Array with the objects inside it with proper format.在我的页面中,但console.log()以正确的格式显示了其中包含对象的数组。

How can This be done?如何才能做到这一点? Any Idea?任何的想法? Have checked a few links from stackoverflow but it's going over my head.已经检查了 stackoverflow 中的一些链接,但它超出了我的脑海。

try尝试

$('div').html('<p>' + JSON.stringify(data) + '</p>')

but i would suggest .append() rather than .html()但我建议 .append() 而不是 .html()

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

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