React Js: Uncaught (in promise) SyntaxError: Unexpected token in JSON at position 0
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");});
}
Uncaught (in promise) SyntaxError: Unexpected token in JSON at position 0 ERROR -- NodeJS, MongoDB, React Application
Express uses precedence when defining routes. I had the following code placed above all of my get routes, therefore overriding them.
app.get("*", function (request, response) {
response.sendFile(path.resolve(__dirname, "../frontend/build", "index.html"));
});
Solution:
I moved those lines of code to the bottom so that the other routes would take precedence. Now, I get JSON in the response instead of the index.html.
How to fix “Uncaught (in promise) SyntaxError: Unexpected token in JSON at position 0” ERROR
"proxy": "http://localhost:8080"
Above line should be outside the Scripts block below. Because "proxy" is not a part of scripts, but rather a port that talks to the react server see details Like SO:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:8080"
How to fix Uncaught (in promise) SyntaxError: Unexpected token in JSON at position 0 ERROR
You are getting a JSON back. I just tried to call
async function get() {
try {
const res = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=079b76b390ad70c628a14a9a141e5992`);
const json = await res.json();
console.log('json', json)
} catch (err) {
console.error('err', err);
}
}
It responds with:
{
"coord": {
"lon": -0.13,
"lat": 51.51
},
"weather": [
{
"id": 520,
"main": "Rain",
"description": "light intensity shower rain",
"icon": "09d"
}
],
"base": "stations",
"main": {
"temp": 285.3,
"pressure": 1004,
"humidity": 93,
"temp_min": 284.15,
"temp_max": 286.48
},
"visibility": 10000,
"wind": {
"speed": 6.2,
"deg": 90
},
"clouds": {
"all": 90
},
"dt": 1571056651,
"sys": {
"type": 1,
"id": 1502,
"message": 0.0096,
"country": "GB",
"sunrise": 1571034113,
"sunset": 1571073060
},
"timezone": 3600,
"id": 2643743,
"name": "London",
"cod": 200
}
You might have missed the http://
part?
Try to use GET and POST, but this error keep using up, Uncaught (in promise) SyntaxError: Unexpected token in JSON at position 0, working on react
You need to add 2 headers Content-Type
and Accept
equal to application/json
.
headers : {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
This will work for you.
Related Topics
!Function(){ }() VS (Function(){ })()
Node.Js Variable Declaration and Scope
JavaScript While Loop Return Value
Code with Classlist Does Not Work in Ie
Chaining .Bind() Calls in JavaScript. Unexpected Result
Case Insensitive Jquery Attribute Selector
Why Does Changing One Array Alters the Other
JavaScript Fuzzy Search That Makes Sense
How to Get the Back Button to Work with an Angularjs Ui-Router State MAChine
Is There a Generic Way to Set State in React Hooks? How to Manage Multiple States
How to Detect with JavaScript/Jquery If the User Is Currently Active on the Page
Does JavaScript Process Using an Elastic Racetrack Algorithm
How to Wrap an Await/Async Try/Catch Block to Every Function
Background-Color Hex to JavaScript Variable
Multiple Conditions for JavaScript .Includes() Method
Get Position/Offset of Element Relative to a Parent Container