首页 > 解决方案 > 如何使用 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);
    }

标签: javascript

解决方案


可能是 ingetData(res) 已经res是一个解析的 JSON 对象,所以这行不是必需的:

let data = JSON.parse(res);

尝试将其更改为

let data = res;

推荐阅读