我该如何解决这个错误 JSON.parse: unexpected character at line 1 column 2 of the JSON data
我正在尝试显示代码链接中的特定 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("");
}
});
任何帮助将不胜感激,谢谢
最佳答案
尝试移除 JSON.parse,
var lastPrice;
function refreshPrice() {
$lastPrice = $('#lastPrice');
$lastPrice.html("");
$.get("https://example.com") //Ticker link
.then(function (data) {
debugger;
lastPrice = data.ticker.last - 100000;
lastPrice.html(lastPrice);
});
}
同时放一个调试器,看看你的 react 是什么。
关于javascript - JSON.解析 : unexpected character at line 1 column 2 of the JSON data (HTML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42594980/