Jquery Javascript push()函数未添加到数组

Jquery Javascript push()函数未添加到数组,jquery,Jquery,我看过其他类似的问题,但到目前为止,似乎没有一个是完全适用的 我正在尝试从数据库中获取的数据生成一个字符串,并将其推送到Pickachoose jquery库的数组中,该库需要JSON,如下所示: var a = [ {"image":"../../1.jpg","caption":"Any donation is appreciated. PikaChoose is free to use!","link":"http://pikachoose.com","title":"Image 1"

我看过其他类似的问题,但到目前为止,似乎没有一个是完全适用的

我正在尝试从数据库中获取的数据生成一个字符串,并将其推送到Pickachoose jquery库的数组中,该库需要JSON,如下所示:

var a = [
  {"image":"../../1.jpg","caption":"Any donation is appreciated. PikaChoose is free to use!","link":"http://pikachoose.com","title":"Image 1"},
  {"image":"../../2.jpg","caption":"Be sure to check out <a href=\"http://www.pikachoose.com\">PikaChoose.com</a> for updates.","link":"http://pikachoose.com","title":"Image 2"},
  {"image":"../../3.jpg","caption":"You can use any type of html you want with PikaChoose","link":"http://pikachoose.com","title":"Image 3"}
 ];
$(".pikachoose").PikaChoose({data:a});
var a=[
{“图片”:./../1.jpg”,“标题”:“感谢您的捐赠。PikaChoose免费使用!”,“链接”:http://pikachoose.com“,”标题“:“图像1”},
{“图像”:“../../2.jpg”,“标题”:“请确保签出更新”,“链接”:”http://pikachoose.com“,”标题“:“图像2”},
{“image”:“../../3.jpg”,“caption”:“您可以在PikaChoose”,“link”中使用任何类型的html”:http://pikachoose.com“,”标题“:“图像3”}
];
$(“.pikachoose”).pikachoose({data:a});
如果我这样硬编码的话,上面的方法就行了

但是,我使用以下代码从mySQL数据库获取数据:

    var a = [];

    $.getJSON("search.cfc?method=x&returnformat=json&queryformat=column",{"id":id},function(res,code){
         for (var i = 0; i<res.ROWCOUNT; i++) {
              var img = res.DATA.IMAGE[i].toLowerCase();
              var cap = res.DATA.CAPTION[i].toLowerCase();
              var lnk = res.DATA.LINK[i].toLowerCase();
              var ttl = res.DATA.TITLE[i].toLowerCase(); 
              var str = '{"image":"' + img + '","caption":"' + cap + '","link":"' + lnk + '","title":"' + ttl + '"}';  
              // str gives, e.g., {"image":["/images/website/retailers/logos/78/serena.jpg"],"caption":["ghsf"],"link":["jhghdfghd"],"title":["rtetuye"]}
              a.push(str); // problem here; doesn't push the string
         }

 });
var a=[];
$.getJSON(“search.cfc?method=x&returnformat=json&queryformat=column”,{“id”:id},函数(res,代码){

对于(var i=0;i您应该推送对象而不是字符串。将
var str=…
更改为

var str = {
    "image": img,
    "caption": cap,
    "link": lnk,
    "title": ttl
}; 

如果您从MySQL数据库获取数据并控制其代码,然后将数据返回为JSON,那么将其拆分并重新生成JSON有什么意义?只需从
search.cfc
中输出正确的格式并跳过客户端处理您应该推送对象而不是字符串。何时检查
a
查看是否有任何内容被推送?是否在
getJSON()之后
call,
a
仍然是空的,因为调用是异步的。在
getJSON
success函数中检查它,但是在循环之后,我希望
a
附加字符串。@amit\u g这可能会导致推送错误的内容,但不会推送任何内容。@PaulRoub,这至少是t的一个问题(可能还有更多)。注释“//str”给出的,例如“似乎表明数据正在返回。它只是没有被转换成正确的格式。“未被推送”可能是基于库中未显示任何内容而不是数组a中未显示任何内容的推断。