我怎样才能解决这个错误JSON.parse: unexpected character at line 1 column 2 of the JSON data
JSON.parse:第1行的JSON数据(HTML)的第2列意外的字符
我试图显示从股票连接特定的JSON内容,但遗憾的是不能让它工作,因为错误不断弹出。
HTML
<input id="currency1" type="text">
<span>Currency1</span>
<input id="currency2" type="text">
<span>Currency2</span>
<div>
<button type="button" onclick="refreshPrice()">
Refresh Price
</button>
<span id="lastPrice"></span>
</div>
从北京时间链接JSON响应
{"ticker":{"high":"16985100","low":"16730900","last":"16879000"}}
JAVASCRIPT
var lastPrice;
function refreshPrice() {
$lastPrice = $('#lastPrice');
$lastPrice.html("");
$.get("https://example.com") //Ticker link
.then(function (data) {
lastPrice = JSON.parse(data).ticker.last - 100000;
lastPrice.html(lastPrice);
});
}
refreshPrice();
$('#currency2').keyup(function() {
currency2Val = parseFloat($(this).val());
if (currency2Val) {
currency1Val = currency2Val * lastPrice;
$('#currency1').val(parseInt(currency1Val));
}
else {
$('#currency1').val("");
}
});
$('#currency1').keyup(function() {
currency1Val = parseInt($(this).val());
if (currency1Val) {
currency2Val = currency1Val/lastPrice;
$('#currency2').val(currency2Val.toFixed(8));
}
else {
$('#currency2').val("");
}
});
任何帮助将不胜感激,谢谢
您响应可能是HTML而不是JSON ......或者它已经被解析,并且你正试图解析一个对象! ...试试'lastPrice = data.ticker.last - 100000;' –
也'lastPrice.html(lastPrice);'如果lastPrice'是一个'Number'(它肯定会是) - 没有意义 - 我认为你的意思是'$ lastPrice.html(lastPrice);' –
这是正确的,你对'$ lastPrice.html(lastPrice)'是正确的,它解决了错误信息。谢谢 – Julius