在下面的代码中,它应该在对象GameThumbs下为r.Showcase
中的26个对象中的每一个存储26个不同的变量
但是,存储的所有内容都是Object {26: "http://t3.rbxcdn.com/1e2476473494bfb202592501a5f86655"}
我的代码没有任何问题,请帮我解决。谢谢(对不起,如果我没有充分描述问题;我的英语不是很好)
function info(id) {
var User = {};
var GameIds = {};
var GameNames = {};
var GameThumbs = {};
$("html").html("");
$.get('http://api.roblox.com/Users/'+id, function(r) {
User['Username'] = r.Username;
}).done(function() {
$.get('http://www.roblox.com/Contests/Handlers/Showcases.ashx?userId='+id, function(r) {
for (var x in r.Showcase) {
GameIds[x] = r.Showcase[x]['ID'];
GameNames[x] = r.Showcase[x]['Name'];
var link = 'http://www.roblox.com/item-thumbnails?params=%5B%7BassetId:'+GameIds[x]+'%7D%5D';
$.get(link, function(d) {
GameThumbs[x] = d[0]['thumbnailUrl'];
});
}
}).done(function() {
console.log(User, GameIds, GameNames, GameThumbs);
});
});
}
请求的样本JSON(link
)
[{"id":45778683,"name":"Temple of the Ninja Masters!","url":"/Temple-of-the-Ninja-Masters-place?id=45778683","thumbnailFinal":true,"thumbnailUrl":"http://t0.rbxcdn.com/6eb2d1f05d120757ae1e72d8e28c918e","bcOverlayUrl":null,"limitedOverlayUrl":null,"deadlineOverlayUrl":null,"limitedAltText":null,"newOverlayUrl":null,"imageSize":"large","saleOverlayUrl":null,"iosOverlayUrl":null,"transparentBackground":false}]
/Contets/Handlers/Showcases.ashx ... 的
{"Error" :"", "Count" :"43", "UserIsAtMaxShowcases":"false", "Showcase" : [{"ID":164081122,"Name":"For ShrekIetsky"}, {"ID":163393918,"Name":"N7"}, {"ID":162493258,"Name":"Hackathon 2014"}, {"ID":158445333,"Name":"bests kings"}, {"ID":157560077,"Name":"Don't be an idiot."}, {"ID":157183481,"Name":"Lots of Thumbnails"}, {"ID":156339175,"Name":"Watch out!"}, {"ID":155669451,"Name":"YES I AM BIASED"}, {"ID":155669434,"Name":"Place_4"}, {"ID":155669424,"Name":"Place_3"}, {"ID":155669403,"Name":"Place_2"}, {"ID":155669381,"Name":"Place_1"}, {"ID":153242188,"Name":"earth2noah"}, {"ID":152827982,"Name":"Buying Egg Merch?"}, {"ID":152563337,"Name":"Inside Mallowpuff's head"}, {"ID":151207481,"Name":"nothin to see here"}, {"ID":151117405,"Name":"Acronym test"}, {"ID":149734880,"Name":"TESTING"}, {"ID":146087560,"Name":"FPS maps"}, {"ID":144140735,"Name":"Old Memories"}, {"ID":144035150,"Name":"Quitting"}, {"ID":142668569,"Name":"Challange Projecto"}, {"ID":140204830,"Name":"Element Obby! [Beta]"}, {"ID":140201014,"Name":"NEVERMIND KIDS C:"}, {"ID":138010986,"Name":"Passage of the Mind"}, {"ID":138006022,"Name":"I see you"}, {"ID":135852345,"Name":"Trade Information!"}, {"ID":135303493,"Name":"Future project"}, {"ID":129885802,"Name":"ololwoaplsiaeo"}, {"ID":129768085,"Name":"SiIencedSong's Place Number: 16"}, {"ID":127537299,"Name":"olopio"}, {"ID":120338485,"Name":"LmaD building!"}, {"ID":120338432,"Name":"Maze Daze [INACTIVE]"}, {"ID":120338402,"Name":"Volcano Survival maps"}, {"ID":120338370,"Name":"[[OpenSource]]"}, {"ID":120338338,"Name":"Yes"}, {"ID":120338296,"Name":"No"}, {"ID":120338264,"Name":"hackathon0n stuff"}, {"ID":118933268,"Name":"-"}, {"ID":118932768,"Name":"Testing"}, {"ID":118573473,"Name":"ROBLOX fame HQ."}, {"ID":118324272,"Name":"Simple spawnroom"}, {"ID":117834273,"Name":"Lightningsurvivor's fort."}] }
答案 0 :(得分:1)
异步代码存在同样的问题。
当您使用for...in
进行迭代时,x
会发生变化。当第一次调用$.get
的回调函数时,for
已完成其迭代,x
现在产生迭代的最后一个值,即大概26岁。
您有两个解决方案:
$.get
调用同步(不好,永远不要这样做); 使用jQuery,例如:
$.each(r.Showcase, function(x) {
...
$.get(link, function(d) {
GameThumbs[x] = d[0]['thumbnailUrl'];
});
});
而不是for (var x in r.Showcase)
。这样,x
是$.each
的回调函数中的局部变量,从$.get
返回时其值将相同。