首先,我注册一个免费帐户并获取密钥:
a52cdce3df0bb71535ee57d8e8c48566
第二,我正在遵循openweathermap的快速入门,请尝试使用“当前天气数据”,这是免费的帐户。
这是我的网址:
https://api.openweathermap.org/data/2.5/weather?q=wuhan,china&appid=a52cdce3df0bb71535ee57d8e8c48566
在邮递员中,数据成功返回: [![在此处输入图片描述] [1]] [1]
但是,在浏览器中,运行代码时,它将返回401: [![在此处输入图片描述] [2]] [2]
代码如下:
const API_KEY = "a52cdce3df0bb71535ee57d8e8c48566";
class App extends React.Component {
getWeather = async e => {
e.preventDefault();
const city = e.target.elements.city.value;
const country = e.target.elements.country.value;
const api_call = await fetch(
"https://api.openweathermap.org/data/2.5/weather??q=${city},${country}&appid=${API_KEY}"
);
const data = await api_call.json();
console.log(data);
};
[1]: https://i.stack.imgur.com/paJD4.png
[2]: https://i.stack.imgur.com/YhbeB.png
}