如何解决解析来自 RestDB 的 JSON 数据以向用户显示信息
使用聊天系统的用户将报告问题,当您登录以获取这些报告时,该问题将发送到 restdb 和管理员。我在 restdb 上使用 Ajax GET 函数来调用这些报告并将它们显示在“显示 Vals”部分中。我一直在尝试不同的东西。我不断收到“Uncaught SyntaxError: Unexpected token o in JSON at position 1”错误。我有一个包含在下面的屏幕截图。只是想知道是否有人可以指导我哪里出错了? 以前我没有包含循环,只有 var string = JSON.parse(response);连同 document.getElementById('displayVals').innerHTML = x;这不会抛出错误,但结果将显示为未定义。
<script>
export default{
methods:{
reportsMethod(){
var x = document.getElementById("report");
console.log(x);
var button = document.getElementById('reportBtn').addEventListener('click',() => {
/* once everything is ok,post info to report db*/
this.$f7.dialog.alert('Report: ' + x +'<br>Sucessfully gotten ');
var settings = {
"async": true,"crossDomain": true,"url": "https://yoururl","method": "GET","headers": {
"content-type": "application/json","x-apikey": "xxxxxxxxxxxxxxxxxxxxx","cache-control": "no-cache"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
var string = JSON.parse(response);
// for(var i = 0; i < string.length; i++){
// var x = string[i];
document.getElementById('displayVals').innerHTML = string;
// }
});
});
}
}
}
</script>
错误:
解决方法
您的代码中有语法错误。代码末尾有额外的花括号。
您可以在编辑器中计算开始和结束大括号的数量。然后你会发现它是否有任何不一致的大括号。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。