Error on GET request to steam market

2019-08-10 02:06发布

问题:

I am attempting to do a GET request to get the item price info for a single item on the steam market.

Here is the exact angularJS script I am using:

<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>

When I try to do this in my browser I get:

Uncaught SyntaxError: Unexpected token : ?currency=3&appid=730&market_hash_name=StatTrak™ P250 | Steel Disruption (Factory New):1

Inside my chrome browser console. I then click the link in error message (?currency=3&appid=730&market_hash_name=StatTrak™ P250 | Steel Disruption (Factory New):1)

And it takes me to a line that is:

{"success":true,"lowest_price":"1,09&#8364; ","volume":"348","median_price":"1,01&#8364; "}

So I actually get the information, but it gives an error and underlines the JSON response in red. Anyone know the issue?

I am using nodejs, express, and angular.

EDIT:

I tried a different URL: https://angularjs.org/greet.php?callback=JSON_CALLBACK&name=Super%20Hero

And it worked with this URL. So im not sure why the original URL I am trying to request is not working. Any ideas on that?

回答1:

When you are using JSONP, it should call JSON_CALLBACK() to process the results. You can refer to Angular JSONP document for more information, it also provides a live demo of how JSON_CALLBACK is used.

So in your case, you should call like below

$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); 
});

If you server handle JSON_CALLBACK correctly, all errors should be gone.

Update

I just look into the 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

They don't support JSONP, and they don't support CORS either. It looks like you can access steam api from Angular.