【问题标题】:displaying JSON data on page with javascript使用 javascript 在页面上显示 JSON 数据
【发布时间】:2012-05-16 11:06:36
【问题描述】:

我终于成功地通过 AJAX 传递数据并以 JSON 形式返回。但是,现在我不知道如何在我的页面上显示它。

这是我的 JS:

$(function() {
    $( "#selectable" ).selectable({
        selected: updatefilters,
        unselected: updatefilters
    });   
    function updatefilters(ev, ui){
        // get the selected filters
        var $selected = $('#selectable').children('.ui-selected');
        // create a string that has each filter separated by a pipe ("|")
        var filters = $selected.map(function(){return this.id;}).get().join("\|");
        $.ajax({
            type: "POST",
            url: 'updatefilters',
            dataType: 'json', 
            data: { filters: filters },
            success: function(data){
                $('#board').replaceWith(data.content);
            }
        });
    }
});

我的模型运行后,它会回显在 firebug 中发现的以下内容:

[{"thread_id":"226","owner_id":"1","subject":"trying to create a HUGE body for thread testi","body":"Firstly, I think 45 characters is perfect for the heading. \n\nHowever, once the body gets huge, I assume that the \"preview\" is going to get a bit out of hand if not truncated. I have truncated code in place...but it doesn't seem to be working.\n\nI'll have to keep testing to ensure we can get it shrunk down to a few hundred characters so as to not completely screw up the page format\n\nyadda yadda yadda...yadda yadda yadda...and more yadda yadda yadda...\n\nBabble Babble Babble Babble Babble \n\nBabble Babble Babble ","timestamp":"2012-05-02 15:29:19","replystamp":"2012-05-02 15:29:53","last_post_id":"1","slug":"226-trying-to-create-a-huge-body-for-thread-testi","replies_num":"1","views":"19"},{"thread_id":"219","owner_id":"3","subject":"testing icons","body":"hellow world","timestamp":"2012-05-01 11:37:08","replystamp":"2012-05-01 11:37:08","last_post_id":"3","slug":"219-testing-icons","replies_num":"0","views":"3"},{"thread_id":"212","owner_id":"1","subject":"This thread is based off entertainm","body":"Yay","timestamp":"2012-04-25 14:07:55","replystamp":"2012-04-25 14:07:55","last_post_id":"1","slug":"212-this-thread-is-based-off-entertainment","replies_num":"0","views":"4"}]

任何关于如何用返回的 JSON 数组中的数据替换 div“板”的建议都会非常有帮助!

【问题讨论】:

    标签: php ajax json jquery-ui codeigniter


    【解决方案1】:

    假设您想在包含主题和正文的表格中显示数据。你可以这样写:

    success: function(data){
        var html = "<table>";
        for (i=0 ; i< data.length ; i++)
        {
            html += "<tr><td>" + data[i].subject + "</td><td>" + data[i].body + "</td></tr>";
        }
        html += "</table";
        $('#board').html(html);
    } 
    

    你明白了。如果您有时间研究更好的解决方案,我还建议您查看模板库 mustache.js。这将使您可以将模板与数据分开,并使用 Javascript 消除 HTML 和数据的丑陋连接。 (虽然是一种更高级的解决方案,您可能只想保持简单。) Mustache.js 引擎允许您像这样编写上面的内容:

    var template = "{{#item}}<tr><td>{{subject}}</td><td>{{body}}</td></tr>{{/item}}";
    var html = "<table>" + mustache.render(template, data) + "</table>";
    $("#board").html(html);
    

    【讨论】:

    • 我最初有一个视图,可以显示直接从 mySQL 查询中获取的数据。有没有人可以使用该视图来显示这些数据?它有很多 HTML……把它放在 javascript 中听起来很困难。
    【解决方案2】:
    success: function(data){
     $.each(data, function(index, value) { 
        $('#board').append(index + ': ' + value + '<br/>'); 
     });
    }
    

    也可以直接从json中获取值

    success: function(data) {
        $('#board').append('Thread Subject' + data.subject); 
    }
    

    【讨论】:

    • 谢谢!它终于做点什么了。虽然它返回这个:0:[object Object] 1:[object Object] 2:[object Object]
    • 你能否给出一个成功的console.log(数据)并将firebug打印的内容粘贴到这里?
    • 我相信它显示的内容与我在问题中发布的内容相同(第二个代码帖子)。我是萤火虫的新手,不知道如何完成你的要求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 2020-01-25
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    相关资源
    最近更新 更多