2015-04-04 65 views
0

我開始用JS編寫。我知道,這可能是語法錯誤,但我不能修復它:/我的簡單市場價格檢查器不起作用

Uncaught SyntaxError: Unexpected token :

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 + '€'); 
}); 

你們能幫助我嗎?

回答

-1

這是你的問題。我跑

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

,並得到這個輸出

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. 

這意味着你不能做到這一點使用JQuery。嘗試使用Steam的Web API:

+0

我不知道如何使用Steam的Web API。你能給我一個獲得物品價格的例子嗎? – Kartm 2015-04-05 20:04:17

+0

據我所知,你有2個選擇:1)如果你有一個域名:http://steamcommunity.com/dev/apikey。 2)如果你使用的是Node.js,PHP等,你可以使用類似https://www.npmjs.com/package/steam-community的東西。對此沒有簡單的答案,這很愚蠢。 – mkaminsky 2015-04-06 00:22:55

+0

沒有**蒸汽** API來獲得市場上物品的價格。 OP正在使用的URL將會很好。無論他是使用API​​還是上面提到的URL,他仍然會收到此錯誤,因爲Steam已經禁止從JavaScript中打開其URL。 Google'JavaScript跨域請求'。有關如何在Steam市場上獲取物品價格的信息,請參閱我的(稍微老一點)回答:http://stackoverflow.com/a/25665733/2634190 – snh 2015-04-10 08:47:51