I’m trying to fetch data from an API using JavaScript’s fetch function. The API is returning JSON data, but I’m having trouble parsing it correctly.
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch(error => {
console.error('Error fetching data:', error);
});
when I run this code, I’m getting the following error in the console.
SyntaxError: Unexpected token < in JSON at position 0
What could be causing this error, and how can I properly parse the JSON data from the API response?
New contributor
chamod kavinda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1