我正在尝试执行 GET 请求来获取 Steam 市场上单个商品的商品价格信息。
这是我正在使用的确切的 angularJS 脚本:
<script>
var app = angular.module('csgo', []);
app.controller('MainCtrl', function($scope, $http) {
$http.jsonp("http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29").success(function(response) {
$scope.values = response;
console.log(response);
});
});
</script>
当我尝试在浏览器中执行此操作时,我得到:
Uncaught SyntaxError: Unexpected token : ?currency=3&appid=730&market_hash_name=StatTrak™ P250 | Steel Disruption (Factory New):1
在我的 Chrome 浏览器控制台内。然后,我单击错误消息中的链接 (?currency=3&appid=730&market_hash_name=StatTrak™ P250 | Steel Disruption(全新出厂):1)
它带我到一行:
{"success":true,"lowest_price":"1,09€ ","volume":"348","median_price":"1,01€ "}
所以我实际上得到了信息,但它给出了一个错误并用红色强调了 JSON 响应。有人知道这个问题吗?
我正在使用 Nodejs、Express 和 Angular。
编辑:
我尝试了不同的 URL:https://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero
并且它可以与此 URL 配合使用。所以我不确定为什么我尝试请求的原始 URL 不起作用。对此有什么想法吗?
最佳答案
当您使用JSONP
时,它应该调用JSON_CALLBACK()
来处理结果。您可以引用Angular JSONP document欲了解更多信息,还提供了live demo如何使用 JSON_CALLBACK
。
所以在你的情况下,你应该像下面这样调用
$http.jsonp("http://steamcommunity.com/market/priceoverview/?callback=JSON_CALLBACK¤cy=3&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29").success(function(response) {
$scope.values = response;
console.log(response);
});
如果服务器正确处理 JSON_CALLBACK
,所有错误都应该消失。
更新
我只是查看Steam Community
There is a lack of support for the JSONP protocol and the API's acceptable usage policy requires that the API key is not to be shared. As described, using this API key inside of a Javascript file (which is downloaded in full source by the client) would be against the policy that Valve has provided
它们不支持 JSONP
,也不支持 CORS
。看起来您可以从 Angular 访问 steam api。
关于angularjs - 向 Steam 市场发出 GET 请求时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30634093/