当从一些API获取时,我得到了TypeError: Failed to fetch
错误。
例如,下面的代码片段中使用的URL是由API提供程序提供的示例。无需密钥即可访问。它在我的浏览器中运行良好,但会在我的代码中抛出错误。
从另一个公共API https://api.punkapi.com/v2/beers/random
中获取数据可以完美地工作。
这些API之间的区别是什么?这些API阻止我从一些API获取数据,而对另一些API有效?
export default function App() {
useEffect(() => {
fetch(
'https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=439d4b804bc8187953eb36d2a8c26a02'
)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => {
console.error(error);
});
}, []);
return null;
}
发布于 2020-07-23 19:21:03
您无法从样本中获取数据,因为由于CORS标头‘Access-Control-Allow-Origin’丢失,导致API被阻止
需要生成接口密钥(https://openweathermap.org/price)并接入接口
//ex:
api.openweathermap.org/data/2.5/weather?appid={your api key}&....
https://stackoverflow.com/questions/63052878
复制相似问题