Om Shanker
Om Shanker

Reputation: 49

Uncaught TypeError: Cannot read property 'length' of undefined when using $.each on JSON data

My code is

var obj = $.parseJSON(json.responseText);
        $.each(obj.collection.response, function (i) {
          $.each(this.PRESENT_STUDENT, function (i, j) {
            $.each(j , function (i, j){
                $('#result-table tr:last').after("<tr><td>"+ j +"</td></tr>");
            })
          })
        })

but when i run this i get error "Uncaught TypeError: Cannot read property 'length' of undefined"

Upvotes: 0

Views: 4867

Answers (1)

R. Oosterholt
R. Oosterholt

Reputation: 8080

Well, you example works: created this fiddle:

$.each(obj.collection.response, function (i, val) {
    // check needed because of 'status' property (see http://jsfiddle.net/6JWE7/4/)
    if (this.PRESENT_STUDENT) {
        $.each(this.PRESENT_STUDENT, function (i, j) {
             $.each(j , function (i, j){
                 $('#result-table tr:last').after("<tr><td>"+ j +"</td></tr>");
             });
        });
    }
});

Upvotes: 1

Related Questions