jQuery $.getJSON 抛出意外令牌
jQuery $.getJSON throws Unexpected Token
我是 JavaScript 初学者。我想使用以下 URL:
从 Steam 市场检索一些数据
https://steamcommunity.com/market/priceoverview/?country=PL¤cy=3&appid=730&callback=?&market_hash_name=Operation%20Vanguard%20Weapon%20Case#
我在浏览器中收到此响应:
{"success":true,"lowest_price":"0,09\u20ac","volume":"1,017","median_price":"0,10\u20ac"}
但我无法让它在 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 + '€');
});
这让我很震惊:
Uncaught SyntaxError: Unexpected token :
这段代码有什么问题?
这是你的问题。我运行
$.ajax( "https://steamcommunity.com/market/priceoverview/?country=PL¤cy=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¤cy=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:https://developer.valvesoftware.com/wiki/Steam_Web_API
问题最终被我解决了。问题是 Steam 不允许在浏览器中使用 JavaScript 代码访问他们的市场数据,因为 Access-Control-Allow-Origin.
我已将我的 JS 代码重写为 PHP 并发送了相同的请求,但这次来自我自己的 WWW 服务器:
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);
}
工作得很好。
我是 JavaScript 初学者。我想使用以下 URL:
从 Steam 市场检索一些数据https://steamcommunity.com/market/priceoverview/?country=PL¤cy=3&appid=730&callback=?&market_hash_name=Operation%20Vanguard%20Weapon%20Case#
我在浏览器中收到此响应:
{"success":true,"lowest_price":"0,09\u20ac","volume":"1,017","median_price":"0,10\u20ac"}
但我无法让它在 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 + '€');
});
这让我很震惊:
Uncaught SyntaxError: Unexpected token :
这段代码有什么问题?
这是你的问题。我运行
$.ajax( "https://steamcommunity.com/market/priceoverview/?country=PL¤cy=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¤cy=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:https://developer.valvesoftware.com/wiki/Steam_Web_API
问题最终被我解决了。问题是 Steam 不允许在浏览器中使用 JavaScript 代码访问他们的市场数据,因为 Access-Control-Allow-Origin.
我已将我的 JS 代码重写为 PHP 并发送了相同的请求,但这次来自我自己的 WWW 服务器:
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);
}
工作得很好。