开发者

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...


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.


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]...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜