2016-11-30 28 views
0

我希望你很好。我正在開發一個涉及在角JS中使用Steam Web API的項目。我試圖從該URL獲取數據:

http://api.steampowered.com/ISteamApps/GetAppList/v2 

當我在谷歌瀏覽器運行我的代碼,它顯示:

Uncaught SyntaxError: Unexpected token : 

我知道有這個問題與CORS(有的網站不打它很好的,所以我試圖解決的是使用JSONP出了什麼事,我丟失或做錯了什麼?

親切的問候,

阿迪

這裏的代碼中的相關片段(我已經排除%的蒸汽網頁API條款我的API密鑰):

var steam_api = "https://api.steampowered.com/ISteamApps/GetAppList/v2"; 
steam_api += "?key=mySteamKey"; 
steam_api += "&format=json&callback=JSON_CALLBACK"; 

$scope.steamGames = {}; 
    $http.jsonp(steam_api) 
    .success(function(data, status, headers, config){ 

    console.log(data); 
    $scope.steamGames = JSON.parse($scope.rawData); 
    $scope.steamSearch = document.getElementById('Search').value; 

    }); 

編輯:只是爲了清楚起見,我還檢查了JSON驗證和JSON,它噴出是有效的。

+0

您可以顯示更詳細的錯誤信息? – Ladmerc

+0

就Chrome的開發人員控制檯顯示的錯誤消息而言。 –

+0

我不知道這是否有幫助,但是當我添加&jsonp =?在steam_api結束時,錯誤消失。聽起來好,我做了一些console.log行,他們確認它實際上跳過了我的.success功能。我將&jsonp取出並再次顯示意外的令牌錯誤。這意味着什麼? –

回答

1

我們有一個答案。 Steam的API不允許你使用CORS向它發送請求(因爲它不允許跨源請求)。如果你通過JSONP發送它,那麼你會像我一樣得到意想不到的令牌錯誤。但是,仍然有希望。

在CodePen上使用教程我能夠向Steam Web API發出請求(使用我在NodeJS中編寫的服務器),並從變量中的響應中捕獲JSON。我的下一步行動是以某種方式將該變量發送給Angular JS,並使用我的HTML中的ng-repeat指令從中獲取正確的值。謝謝大家的幫助。

編輯:這裏的鏈接到CodePen教程與所需的NodeJS/ExpressJS代碼一起: https://codepen.io/johnchristopherjones/post/how-do-i-use-the-steam-api-in-my-web-app

app.get('/steam/civ5achievements', function(httpRequest, httpResponse) { 
    // Calculate the Steam API URL we want to use 
    var url = 'http://api.steampowered.com/ISteamUserStats/GetSchemaForGame/' + 
     'v2/?key=YOURSTEAMAPIKEYHERE&appid=8930'; 
    request.get(url, function(error, steamHttpResponse, steamHttpBody) { 
     // Once we get the body of the steamHttpResponse, send it to our client 
     // as our own httpResponse 
     httpResponse.setHeader('Content-Type', 'application/json'); 
     httpResponse.send(steamHttpBody); 
    }); 
});