我有这个json
{"suggestions":["M.I.A.","M.","Mindless Self Indulgence","The Cure","Telefon Tel Aviv","M","J. Ralph","Jason Mraz","Carbon Based Lifeforms","Cycle of Pain","Chantal Kreviazuk","-M-","ayumi hamasaki","R.E.M.","Donny McCaslin","Penfold","HEALTH","R. Kelly","DJ Khaled","Eminem","Spose","T.I.","The Lonely Island","H.I.M. (His Infernal Majesty)","Dropkick Murphys","Taylor Swift"],"query":"m"}
我从这个ajax电话中获取
$.getJSON('<%= ajax_path("artistName") %>', req, function(data) {
//create array for response objects
var suggestions = [];
console.log(data);
//process response
$.each(data, function(i, val){
suggestions.push(val.name);
});
console.log(suggestions);
//pass array to callback
add(suggestions);
});
},
为什么我的建议仍为空
答案 0 :(得分:2)
您不必循环,suggestions
已经是一个可供使用的数组,所以请替换它:
var suggestions = [];
console.log(data);
//process response
$.each(data, function(i, val){
suggestions.push(val.name);
});
console.log(suggestions);
有了这个:
var suggestions = data.suggestions;
console.log(suggestions);
然后,传递给add()
的数组将获得JSON响应的结果。