2014-05-03 152 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

相關問題