异步错误:JSON中位置0上的意外令牌<

时间:2018-11-01 02:18:07

标签: javascript

单击按钮时出现问题。

此错误仅在我添加后出现

async (e) => {
e.preventDefault();

在添加之前,它工作正常。

我的App.js是:

import React from "react";
import Titles from "./components/Titles.js";
import Form from "./components/Form.js";
import Weather from "./components/Weather.js";

const API_KEY = "6726a7ca2675f6208f516878e6472f36";

class App extends React.Component {
  getWeather = async (e) => {
    e.preventDefault();
    const api_call = await fetch(`api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=${API_KEY}`);
    const data = await api_call.json();
    console.log(data);
  }
  render() {
    return (
      <div>
        <Titles />
        <Form getWeather={this.getWeather}/>
        <Weather />
      </div>
    );
  }
}

export default App;

我的Form.js是:

import React from "react";

class Form extends React.Component {
    render() {
        return (
            <form onSubmit={this.props.getWeather}>
                <input type="text" name="city" placeholder="City..."/>
                <input type="text" name="Country" placeholder="Country..."/>
                <button>Get Weather</button>
            </form>
        );
    }
};

export default Form;

我要做的就是单击按钮时记录返回的API数据。因此,单击按钮后,我需要停止页面刷新。

0 个答案:

没有答案