希望你一切都好。我正在开发一个项目,涉及在 Angular JS 中使用 Steam Web API。我正在尝试从此 URL 获取数据:
http://api.steampowered.com/ISteamApps/GetAppList/v2
当我在 Google Chrome 中运行代码时,它显示:
Uncaught SyntaxError: Unexpected token :
我知道 CORS 存在这个问题(有些网站对此不太满意,所以我尝试使用 JSONP 来解决这个问题。我是否遗漏或做错了什么?
亲切的问候,
阿迪
以下是相关代码片段(我已根据 Steam Web API 条款排除了我的 API key ):
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 是有效的。
最佳答案
我们有答案。 Steam 的 API 不允许您使用 CORS 向其发送请求(因为它不允许跨源请求)。如果您通过 JSONP 发送它,那么您会像我一样收到意外的 token 错误。然而,仍然有希望。
使用 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);
});
});
关于javascript - Angular JS 中的 Steam Web API : Issue with the GetAppList JSON and $http. jsonp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40896326/