0

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&currency=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&currency=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?

Kartm
  • 39
  • 1
  • 11

2 Answers2

0

Here's your problem. I ran

$.ajax( "https://steamcommunity.com/market/priceoverview/?country=PL&currency=3&appid=730&callback=?&market_hash_name=Operation%20Vanguard%20Weapon%20Case" )
  .done(function() {
    alert( "success" );
  })
  .fail(function(jqXHR, textStatus) {
console.log(jqXHR );
});

And got this output

XMLHttpRequest cannot load https://steamcommunity.com/market/priceoverview/?country=PL&currency=3&appid=730&callback=?&market_hash_name=Operation%20Vanguard%20Weapon%20Case. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fiddle.jshell.net' is therefore not allowed access.

This means that you can't do this using JQuery. Try using Steam's Web API: https://developer.valvesoftware.com/wiki/Steam_Web_API

mkaminsky
  • 353
  • 2
  • 10
  • I have no idea how to use Steam's Web API. Can you give me an example for getting item's price? – Kartm Apr 05 '15 at 20:04
  • 1
    As far as I can tell, you have 2 options: 1) if you have a domain: http://steamcommunity.com/dev/apikey. 2) if you are using Node.js, PHP, etc. you could use something like https://www.npmjs.com/package/steam-community . There is no easy answer to this, which is kind of stupid. – mkaminsky Apr 06 '15 at 00:22
  • 1
    There is no **Steam** API to get the price of an item on the market. The URL that OP is using will do just fine. Whether he is using the API or the above mentioned URL, he will still receive this error because Steam has disallowed hitting their URL's from JavaScript. Google 'JavaScript cross domain requests'. See my (somewhat older) answer on exactly how to get the price of an item on the Steam market place here: http://stackoverflow.com/a/25665733/2634190 – Stephen Lake Apr 10 '15 at 08:47
0

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&currency=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.

Kartm
  • 39
  • 1
  • 11