Click here to Skip to main content
15,790,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using fetch API to get some API JSON data from a remote server and its returning my react index.html file?

What I have tried:

I tried using response.json() but got an unexpected token error so I changed to text to find that its returning index.html

  .then((response) => response.json())
  .then((data) => console.log(data))

  .then((response) => response.text())
  .then((data) => console.log(data))
Updated 25-Jan-23 15:58pm

1 solution

its returning my react index.html file
That happens when your server is unable to route the request to the proper router to handle it, and provide a response. If you can debug your server (if you are developing it at the same time!) you will see why the request goes to a fallback handler.

See here:[^]

You will have something that handles a wildcard to provide the index.html file as a response.
I tried using response.json() but got an unexpected token
That is because the JSON files do not start with a "<"; which is the symbol that you find a lot in the HTML documents. Since your server returns an HTML document, it starts with an angle bracket, causing the JSON parser to complain.

Your best bet is to solve the routing problem in your system.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900