javascript - JSON.解析 : unexpected character at line 1 column 2 of the JSON data (HTML)

标签 javascript jquery json

我该如何解决这个错误 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/

相关文章:

javascript - 如何在HTML 5播放器中获得视频质量控制

javascript - nodejs操作后断开mongoose连接

javascript - 这个 JavaScript 登录不起作用

javascript - 如何使用 jQuery 设置/获取隐藏字段的完整值

php - 图库缺少目录中的文件 - 如何从目录中检索所有文件

php - 如何编写 PHP 脚本来获取给定用户在 Twitter 中的关注者数量?

javascript - 为什么要使用 JSON.parse(decodeURIComponent(staticString))?

javascript - typescript :仅测试 Nan、null 和 undefined

javascript - 如何使 jquery 对话框与 themeroller 主题一起使用?

python - append() 算法错误地追加