GET API_Link net :: ERR_FAILED --------未捕获(承诺中)TypeError:无法获取

时间:2020-03-10 12:42:30

标签: javascript reactjs fetch

我试图从openweathermap.org获取API,但我仍然收到错误:

  • 获取API_Link网络:: ERR_FAILED
  • 未捕获(承诺)的TypeError:无法获取

这是我的代码:

import React, { useState } from 'react';
import Form from './components/form'
import Forecast from './components/forecast'




const App = (props) => {
  const api_key = 'b6907d289e10d714a6e88b30761fae22';
  const [error, setError] = useState("")
  const [temperature, setTemperature] = useState("")
  const [humidity, setHumidity] = useState("")
  const [city, setCity] = useState("")


const getWeather = async (e) => {
  console.log('jj')
  const city = e.target.elements.city.value;
  e.preventDefault();
  const api_call = await fetch(`https://samples.openweathermap.org/data/2.5/weather?q=${city}&appid=${api_key}`);
  const response = await api_call.json();
  console.log('jj')
  if(city){
    setTemperature(response.main.temp)
    setHumidity(response.main.humidity)
    setCity(response.name)
  }else{
    setError('please fill out input error')
  }

}
return (
  <div>
<Form loadWeather={getWeather}/>
<Forecast 
temperature={temperature}
humidity={humidity}
city={city}
error = {error}/>
  </div>
)
}

我使用其他api做到了

0 个答案:

没有答案