How to fix Unexpected token o in JSON at position 1 using JavaScript?

Viewed 23017

This question is seems to be already answered but my scenario is different. I am getting Unexpected token o in JSON at position 1 everytime. Can anyone help me to fix this.Thanks

here is my code

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);
    }
2 Answers

It could be that in getData(res) the res is already a parsed JSON object so this line is not necessary :

let data = JSON.parse(res);

Try change it to

let data = res;
Related