简体   繁体   中英

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();

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...

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM