0

我对返回的 JSON 数组进行了一些编辑,现在它破坏了代码。本质上,我所做的只是把它变成一个多维数组(2?)

JS

$(function() { ...
     ...$.ajax({
            type: "POST",
            url: 'updatefilters',
            dataType: 'json', 
            data: { filters: filters, page: page },
            success: function(data){
                html = "<table class='board'>";
                html += "<table class='board'>";
                html += "   <tr>";
                html += "           <th width='7.5%'>Author</th>";
                html += "           <th width='5%'><img src='../img/board/icons/category_icon.png' alt='category_icon'/></th>";
                html += "           <th width='5%'>Tag</th>";
                html += "           <th width='50%'>Subject</th>";
                html += "           <th width='7.5%'>Replies/Views</th>";
                html += "           <th width='15%'>Last Post</th>";
                html += "  </tr>";
                for (i=0 ; i < data[threads].length ; i++)
                {
                    html += "<tr><td>" + data[threads][i].username + "";
                }
                html += "</table></div>";
                $('#board').html(html); 
            } ...

返回的 JSON 数据:

{"0":"blurb","filter":["blurb"],"threads":[{"thread_id":"234","owner_id":"3","subject":"Blurb(?) is in Heavy Development!","body":"Please be aware that this site is still in heavy development. What you are viewing here is a prototype, not meant to be seen as a final product. if you have comments or suggestions, please send it to rickymm3@gmail.com\n\nThank You!","timestamp":"2012-05-11 08:02:28","replystamp":"2012-05-11 08:02:28","last_post_id":"3","slug":"234-blurb-is-in-heavy-development","replies_num":"0","views":"1","username":"guest"}]}

在 JS 代码的 FOR 循环中,data[threads] 是未定义的?是否有原因 data[threads][i] 不起作用?

4

3 回答 3

4

您没有任何名为threads.

你的意思是data.threads

于 2012-05-11T19:24:58.667 回答
4

data.threads是一个只有一个单元格的数组(其中包含一个对象)

不要使用data[threads]:您可以使用引号data.threadsdata["threads"]引号

于 2012-05-11T19:27:27.497 回答
3

采用

data.threads

或者

data["threads"]
于 2012-05-11T19:25:26.950 回答