[英]Unexpected token React / JSON
function WeatherApp() {功能天气应用(){
const [weather, setWeather] = useState("");
useEffect(() => {
getWeather();
}, []);
const getWeather = async () => {
const response = await fetch(`api.openweathermap.org/data/2.5/weather?q=Krakow&appid=5eeb633dfbff4cd6c565e326670f0d0d&units=metric`);
const data = await response.json();
setWeather(data.weather);
}
return (
<div className="main-cointainer">
<form className="search-form">
<input type="text" className="search-bar"/>
<button type="submit" className="search-button" >Check</button>
</form>
<p>{weather}</p>
</div>
);
} }
I know there are simillar questions but I tried to manage it and didn't work.我知道有类似的问题,但我试图解决它并没有奏效。 I'm really stucked and don't know how to solve this problem.我真的被困住了,不知道如何解决这个问题。 I tried another port, put 'Content-Type': 'application/json', 'Accept': 'application/json', changing my code for many diffrent ways but nothing.我尝试了另一个端口,输入 'Content-Type': 'application/json', 'Accept': 'application/json',以多种不同的方式更改我的代码,但什么也没有。 Network didn't show anything wrong, only console says "WeatherApp.jsx:16 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0" after my getWeather const.网络没有显示任何错误,只有控制台在我的 getWeather const 之后显示“WeatherApp.jsx:16 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0”。
Can anyone help me?谁能帮我?
您需要使用https://
协议修复 fetch url:
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?q=Krakow&appid=5eeb633dfbff4cd6c565e326670f0d0d&units=metric`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.