【发布时间】:2021-07-28 04:28:29
【问题描述】:
我知道这是一个典型的错误,但我无法弄清楚,问题出在哪里?浏览器中的链接以 JSON 结构正常打开,但在weatherRequest.json() 中我什至收到错误Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0。
请帮忙...
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();
【问题讨论】:
-
您在第 2 行的字符串周围使用了错误的引号...使用 '(单引号)而不是 `(反引号)
-
@panther 这没有任何意义。
-
获取 JSON 的结果是否正确?
-
你的url需要https。
-
api.openweathermap.org/data/2.5/forecast?q=München,DE&appid=my_key是相对路径,所以前面会加上当前域。因此,您将从您的域请求该路径,这很可能会返回一个 html 错误页面。该错误页面以<开头。您需要写//api.openweathermap.org/data/2.5/forecast?q=München,DE&appid=my_key、http://api.openweathermap.org/data/2.5/forecast?q=München,DE&appid=my_key或https://api.openweathermap.org/data/2.5/forecast?q=München,DE&appid=my_key
标签: javascript json async-await