I'm a JavaScript beginner. I want to retrieve some data from Steam Market using the following URL:
https://steamcommunity.com/market/priceoverview/?country=PL¤cy=3&appid=730&callback=?&market_hash_name=Operation%20Vanguard%20Weapon%20Case#
I get this response in my browser:
{"success":true,"lowest_price":"0,09\u20ac","volume":"1,017","median_price":"0,10\u20ac"}
But I can't get it to work in JS.
var amount = prompt("How many cases do you have?\t");
$.getJSON("http://steamcommunity.com/market/priceoverview/?country=PL¤cy=3&appid=730&callback=?&market_hash_name=Operation%20Vanguard%20Weapon%20Case#",
function(json) {
var raw_price = json.lowest_price;
var price = raw_price.split('&')[0];
var price_total = price*parseInt(amount);
alert(price_total + '€');
});
It just throws me:
Uncaught SyntaxError: Unexpected token :
What's wrong with this code?
Answer
The problem has been eventually solved by me. The issue was that Steam didn't allow to access their market data using a JavaScript code in a browser because of Access-Control-Allow-Origin.
I have rewritten my JS code to PHP and sent the same request, but this time from my own WWW server:
function get_price() {
$url = "https://steamcommunity.com/market/priceoverview/?country=PL¤cy=3&appid=730&callback=?&market_hash_name=Operation%20Vanguard%20Weapon%20Case";
$json = file_get_contents($url);
$price = json_decode($json);
$price_case = $price->{"lowest_price"};
return number_format($price_case, 2);
}
Worked like a charm.
No comments:
Post a Comment