javascript - 如何使用 JavaScript 在位置 1 处修复 JSON 中的意外标记 o?
问题描述
这个问题似乎已经得到回答,但我的情况不同。我每次都在位置 1 处获得 JSON 中的 Unexpected token o。谁能帮我解决这个问题。谢谢
这是我的代码
function send()
{
let detail = new FormData();
detail.append("start",true);
detail.append("user",'david');
postRequest("https://myjson-url/game.php?"
,detail, getData);
}
function getData(res)
{
let data = JSON.parse(res);
console.log(data);
}
//async function
async function checkStatus(res) {
if (!res.ok) {
throw new Error(await res.text());
}
return res;
}
//my post request
function postRequest(url, info, func){
fetch(url, {method: "POST", body: info})
.then(checkStatus)
.then(func)
.catch(console.error);
}
解决方案
可能是 ingetData(res)
已经res
是一个解析的 JSON 对象,所以这行不是必需的:
let data = JSON.parse(res);
尝试将其更改为
let data = res;
推荐阅读
- c - 在 C 中遍历数组或字符串的最佳方法是什么?
- apache-spark - 无法使用带火花的 ROW FORMAT DELIMITED 加载到 Hive 镶木地板表
- html - 元素仅显示一次后
- c# - 如何过滤掉列表中的项目?
- c++ - 采用 std::initializer_list 的构造函数优于其他构造函数
- python - 使用 django 在 aws 中加载 psycopg2 模块时出错
- string - 旋转字符串以找到等于 K 的汉明距离
- apache-drill - 使用 ABFS 提供程序创建表时,方案“null”没有文件系统
- firebase - 为什么我的 transaction() 函数不起作用?就我而言,我想增加我的点击数据
- python-3.x - 在 Python 中使用 multiprocessing.Pool 和返回自定义对象的函数