开发者

jquery ajax call to rails not returning an array when one record returns

The problem I have is that when I make the AJAX call, and there is more than one record, an array is returned and I can call list[0] or whatever to get the first. If I make the same call and receive a single object I get the object back rather than an array of one.

Any ideas how I can handle this?

UPDATE: Used these functions

function isArray(obj) {

return (obj.constructor.toString().indexOf("Array") != -1);

}

function getArray(obj) {

if(obj.constructor.toString().indexOf("Array") != -1){

开发者_运维技巧     return    obj;

} else {

        var myArr = [1];

        myArr[0]=obj;

        return myArr;

}

}


you could check it with javascript:

if ( list.length > 1 ) {
   list[0];
} else {
  list;
}

you could also do something like that on server side:

@entries = [@entries] unless @entries.is_a?(Array)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜