Max Wolfen
Max Wolfen

Reputation: 2033

Error: Unexpected token < in JSON at position 0

I understand that this is a typical error, but I cannoty figure out, where is the problem located? The link in browser opens normally with JSON structure, but in weatherRequest.json() i even got an error Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0.

Help, please...

let fetchWeather = async () => {
    const weatherRequest = await fetch(`api.openweathermap.org/data/2.5/forecast?q=München,DE&appid=my_key`);
    const weatherStore = await weatherRequest.json();
    console.log('weatherStore', weatherStore);
}

fetchWeather();

Upvotes: 2

Views: 865

Answers (1)

Sandeep Amarnath
Sandeep Amarnath

Reputation: 6916

This happened to me. Two fixes helped me resolve this issue.

  1. Used axios instead of fetch. (Not sure why fetch didn't help)

Install -> npm install axios

Use -> import axios from 'axios';

  1. Prefixed the api url with https:// like this https://api.openweathermap.org...

enter image description here

Upvotes: 1

Related Questions