Scott
Scott

Reputation: 4200

Javascript giving undefined error although it seems like everything is working

success: function (result) {
                    var messages = result;
                    for(var i=0;i<=messages.length;i++) 
                        jQuery("#responseMessages").jqGrid(
                                                            'addRowData',
                                                            i+1,
                                                            {distance:messages[i].distance,age:messages[i].age,message:messages[i].message}
                                                          );
                }

So this is my success callback function of an Ajax call to my mvc app. It then loops through the response and enters each row in a JQgrid. However, I'm getting this error on my page:

messages[i] is undefined

even though all the rows are there and it seems to work fine...

Upvotes: 0

Views: 176

Answers (2)

Šime Vidas
Šime Vidas

Reputation: 185893

Usually, success arguments are strings. I assume that it is a string in your case too. In that case, you cannot perform index lookups [i]...

Upvotes: 0

alex
alex

Reputation: 490153

You probably want to remove the = in <= in your for loop.

jsFiddle.

Seeing as you are using jQuery, you could also use its $.each() to iterate over that.

Upvotes: 5

Related Questions