TypeError:无法获取未定义或空引用的属性“length”

时间:2015-03-08 05:36:06

标签: jquery asp.net json gridview

我有一个我从JSON对象转换过的JavaScript数组。而且,我想在GridView中绑定这个JavaScript数组。但它显示错误“无法获得属性'长度'未定义或空引用'。我的代码是这样的:

 $("#gridview2").empty();

            var obj = $.parseJSON(data);

            if (obj.d.length > 0) {

                $("#gridview2").append("<table><tr><th>SLNo</th><th>Date</th><th>Project Id</th><th>Priority</th><th>QA priority</th><th>reported by</th><th>module</th><th>assigned to</th><th>reassigned to</th><th>module</th><th>description</th></tr></table>");
                for (var i = 0; i < obj.d.length; i++) {

                    $("#gridview2").append("<tr><td>" +
                    data.d[i].SLNo + "</td> <td>" +
                    data.d[i].date + "</td> <td>" +

                    data.d[i].projectName + "</td> <td>" +
                    data.d[i].priority + "</td> <td>" +
                    data.d[i].qapriority + "</td> <td>" +
                    data.d[i].reportedby + "</td> <td>" +
                    data.d[i].module + "</td> <td>" +
                    data.d[i].assignedto + "</td> <td>" +
                    data.d[i].reassignedto + "</td> <td>" +
                    data.d[i].status + "</td> <td>" +
                    data.d[i].description + "</td></tr>");
                }                   
            }

HTML Gridview:

 <asp:GridView runat="server" ID="gridview2" CssClass="grid_format">                                
 </asp:GridView>

1 个答案:

答案 0 :(得分:0)

var obj = $.stringify(data);

尝试使用此代码:

 $("#gridview2").empty();
            var obj = $.stringify(data);
            if (obj.d.length > 0) {
                $("#gridview2").append("<table><tr><th>SLNo</th><th>Date</th><th>Project Id</th><th>Priority</th><th>QA priority</th><th>reported by</th><th>module</th><th>assigned to</th><th>reassigned to</th><th>module</th><th>description</th></tr></table>");
                for (var i = 0; i < obj.d.length; i++) {
                    $("#gridview2").append("<tr><td>" +
                    obj.d[i].SLNo + "</td> <td>" +
                    obj.d[i].date + "</td> <td>" +
                    obj.d[i].projectName + "</td> <td>" +
                    obj.d[i].priority + "</td> <td>" +
                    obj.d[i].qapriority + "</td> <td>" +
                    obj.d[i].reportedby + "</td> <td>" +
                    obj.d[i].module + "</td> <td>" +
                    obj.d[i].assignedto + "</td> <td>" +
                    obj.d[i].reassignedto + "</td> <td>" +
                    obj.d[i].status + "</td> <td>" +
                    obj.d[i].description + "</td></tr>");
                }                   
            }