简体   繁体   English

错误:位置 0 处的 JSON 中出现意外标记 <

[英]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 .浏览器中的链接以 JSON 结构正常打开,但在weatherRequest.json()我什至得到了一个错误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 .使用axios而不是fetch (Not sure why fetch didn't help) (不知道为什么 fetch 没有帮助)

Install -> npm install axios安装 -> npm install axios

Use -> import axios from 'axios';使用 -> import axios from 'axios';

  1. Prefixed the api url with https:// like this https://api.openweathermap.org...像这样https://api.openweathermap.org...https://前缀 api url https://api.openweathermap.org...

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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