Skip to content
Advertisement

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

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

Advertisement

Answer

add json() let data = res.json();

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement