Error resolution of unexpected token in JSON at position 0

This error, or a similar error, can occur when you send an HTTP request, perhaps using a FETCH or other Ajax library.
then I will explain this is caused by what, how should we solve these problems
1.
These errors occur when you send a request to the server that returns a value that is not JSON but is parsed using JSON methods. The code that does this might look like this.
the fetch (‘/users’). Then (res = & gt; Res.json ())
The actual request is fine, it gets a return value, and the key to the problem is res.json(). Parse
json. parse( not a JSON string )>arse
json. parse( is not a JSON string );
json.parse () is essentially the same as res.json(), so the error cases are the same.
3. Invalid JSON
unexpected token o in JSON at position 1″
error prompt Some differences will vary depending on the server return
The symbol or location it prompts may be different, but the reason for it is the same: all the JSON your code parses is not really valid JSON.
> Here are some errors I’ve seen:
Unexpected token < in JSON at position 1
Unexpected token p in JSON at position 0
Unexpected token d in JSON at position 0
4.
With fetch, you can use res.text() instead of res.json() to get the text string itself. Alter your code to read something like this, And check the console to see what’s causing the problem:
first prints out the return value. If you’re using fetch, you can use res.text() instead of res.json() to get the string. Convert your code to something like this and look at the print to see what went wrong.
fetch(‘ /users’)
/. Then (res =>; res.json()) // comment this out for now
.then(res => res.text()) // convert to plain text
.then(text => Console. log(text)) // Then log it out
Note that methods like res.json() and res.text() are asynchronous. So you can’t print out their return values directly, which is why console.log must be inside the parentheses of.then.
5. Is it the server?
server returns HTML instead of JSON for several reasons:
The requested URL does not exist. The server returns the 404 page as HTML. You may have code errors in the request (like /user instead of /users), or errors in the server code.
When a new route is added, the server needs to be restarted. Get (‘ /users’,…) : app.get(‘ /users’…) Route, but without a restart, the server will not respond to the new route address.
client proxies are not set: If you are using a Webpack Dev server like the Create React App, you can set a proxies that point to the back-end server.
API root URL is /. If you are using proxies through Webpack or the Create React App, make sure your API route is not at the root level /. This will confuse the proxy server and you will get an HTML return instead of your API request. You can put a prefix like/API/in front of such as.
is a 404 page?(This may be a missing address or a mistyped code).
Is this the page for index.html?(It could be a missing address or an agent configuration error)
If everything looks good (new address, the server doesn’t restart), and then restart the front-end and back-end server, see if problem solved italic style * *
from: https://segmentfault.com/a/1190000017545154

Read More: