close

React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Error Occurs?

Today I get the following error React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 in javascript.

How To Solve React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Error ?

  1. How To Solve React Js: Uncaught (in promise) SyntaxError: Unexpected token

    To Solve React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Error I was getting the same error, for me, it was because API was just returning a string however in fetch call I was expecting json :
    response => response.json()

  2. React Js: Uncaught (in promise) SyntaxError: Unexpected token

    To Solve React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Error I was getting the same error, for me, it was because API was just returning a string however in fetch call I was expecting json :
    response => response.json()

Solution 1

Add two headers Content-Type and Accept to be equal to application/json.

handleGetJson(){
  console.log("inside handleGetJson");
  fetch(`./fr.json`, {
      headers : { 
        'Content-Type': 'application/json',
        'Accept': 'application/json'
       }

    })
    .then((response) => response.json())
    .then((messages) => {console.log("messages");});
}

Solution 2

I was getting the same error, for me, it was because API was just returning a string however in fetch call I was expecting json :

response => response.json()

Returning json from API resolved the issue for me, if your API is not supposed to return json then simply don’t do response.json()

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment