简体   繁体   English

意外令牌 React / JSON

[英]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.

相关问题 反应 - JSON 中的意外标记 u 在位置 5 - React - Unexpected token u in JSON at position 5 反应 JS:意外的令牌 &lt; 在 JSON 在 position 0 - React JS: Unexpected token < in JSON at position 0 React Native - JSON 中的意外标记 U 在位置 0 - React Native - Unexpected token U in JSON at position 0 “SyntaxError: Unexpected token &lt; in JSON at position 0”在反应 js - "SyntaxError: Unexpected token < in JSON at position 0" in react js 在位置 0 错误处反应 JSON 中的意外令牌 - React unexpected token in JSON at position 0 error 未处理的拒绝(SyntaxError):JSON中位置0上的意外令牌&lt;React - Unhandled Rejection (SyntaxError): Unexpected token < in JSON at position 0 React 错误:语法错误:React Native中位置0处JSON中的意外令牌i - Error : SyntaxError: Unexpected token i in JSON at position 0 in React Native 使用 SyntaxError 调试 React/Flask 连接:Unexpected token &lt; in JSON at position 0 - Debugging React/Flask connection with SyntaxError: Unexpected token < in JSON at position 0 如何在反应钩子中修复 position 0 的 json 中的禁止和意外令牌 - How to fix the forbidden and unexpected token in json at position 0 in react hooks SyntaxError Unexpected token &lt; in JSON at position 0 in react.js API 导航 - SyntaxError Unexpected token < in JSON at position 0 in react.js API navigation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM