angularjs - 向 Steam 市场发出 GET 请求时出错

标签 angularjs node.js express get jsonp

我正在尝试执行 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&#8364; ","volume":"348","median_price":"1,01&#8364; "}

所以我实际上得到了信息,但它给出了一个错误并用红色强调了 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&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); 
});

如果服务器正确处理 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/

相关文章:

javascript - 在浏览器中使用 axios 处理重定向

javascript - 我可以使用 express.js 创建 web api 而没有安装 node.js 吗?

node.js - Node.js 和 AngularJS 中的 CORS

node.js - 如何在 Node.js Express 中实际分离访问日志和错误日志?

angularJS $compile 未定义

node.js - npm CA 证书的位置

javascript - 异步请求进入for循环angular.js

node.js 服务器和 HTTP/2 (2.0) 与 express.js

javascript - 在 angularjs 中调用 http (RESTFUL WebAPI) 的正确方法

angularjs - Angular 重复、跨列突破