2014-05-03 107 views
0

因此,通过Steam Web API获取JSON Steam游戏列表。当我尝试通过Chrome控制台直接从JSON获取媒体资源时,这非常好。但是当我在我的代码中执行相同的行时,它会抛出Uncaught TypeError: Cannot read property 'x' of undefined('x'代表我尝试获得的任何属性)。“Uncaught TypeError:尝试从对象获取属性时无法读取属性'undefined''

key = "lotsofrandomnumbersandletters"; 
id = "steamprofileid"; 
var gameList = $.getJSON("http://www.corsproxy.com/api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=" + key + "&steamid=" + id + "&format=json"); 
var gameCount = gameList.responseJSON.response.game_count; 

回答

2

$.getJSON()是异步的,你需要访问一个回调的结果......

$.getJSON("http://www.corsproxy.com/api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=" 
     + key + "&steamid=" + id + "&format=json", function (gameList) { 
    var gameCount = gameList.responseJSON.response.game_count; 
}); 
+0

我已将它更改为此'var key =“XXXXXXXXXXXXXXXXXX”; var id =“XXXXXXXXXXXXXXXX”; $ .getJSON(“http://www.corsproxy.com/api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=”+ key +“&steamid =”+ id +“&format = json”,function( gameList){ \t var gameCount = gameList.responseJSON.response.game_count; });'它仍然给出一个错误('不能读取属性'响应'...')。我究竟做错了什么? – 730

0

解决它使用安东尼的答案,但在宣布gameCount$.getJSON

相关问题