javascript - openweathermap 上的 5 天天气预报未给出预期结果

标签 javascript fetch-api openweathermap

我正在尝试使用 JS Fetch API 获取 5 天的天气预报,尽管我按照文档中的说明传递了 cnt=5,但我仍然只获取当前天气。我错过了什么吗?

fetch('https://api.openweathermap.org/data/2.5/weather?q=' + city+ '&appid=' + key+'&cnt=5') 

我已经做了足够的研究,但无法弄清楚我到底在哪里做错了。感谢您的帮助。

const key = '**** Your API Key Here ****';

function weatherForecast(city) {
    fetch('https://api.openweathermap.org/data/2.5/weather?q=' + city+ '&appid=' + key+'&cnt=5')  
    .then(function(resp) {
        return resp.json() 
    })
    .then(function(data) {
        console.log('--->'+(JSON.stringify(data)));
        drawWeather(data);
    })
    .catch(function() {
        // catch any errors
    });
}

function drawWeather( d ) {
    var celcius = Math.round(parseFloat(d.main.temp)-273.15);
    var fahrenheit = Math.round(((parseFloat(d.main.temp)-273.15)*1.8)+32);
    var description = d.weather[0].description; 
    
    document.getElementById('description').innerHTML = description;
    document.getElementById('temp').innerHTML = fahrenheit + '°';
    document.getElementById('location').innerHTML = d.name+' '+d.sys.country;
}



//Event Listeners on button click
document.addEventListener("DOMContentLoaded", () => {
    // Handling button click
    document.querySelector(".button-search").addEventListener("click", () => {
        const searchedCity = document.querySelector('.text-search');
        console.log(searchedCity.value);
        if(searchedCity.value){
            weatherForecast(searchedCity.value);
        }       
   }) 
 });
body {
    font-family: 'Montserrat', sans-serif;
    font-weight: 400;
    font-size: 1.3em;
    height: 100vh;
}
h1 {
    margin: 0 auto;
    font-size: 2.2em;
    text-align: center;
    font-size: 10em;
}

.main-container,.search-component{
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 2em;
}

.text-search{
    width: 100%;
    max-width: 280px;
    padding: 10px 15px;
    border: solid blueviolet;
    color: #313131;
    font-size: 20px;
    font-weight: 300;
    transition: 0.2s ease-out;
}

.button-search{
   font-size: 32px;
}
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Current Weather</title>
  <link href="https://fonts.googleapis.com/css?family=Montserrat:400,900" rel="stylesheet">
  <link href="style.css" rel="stylesheet"> 
</head>
<body>
    <div class="search-component">
        <input type="text" autocomplete="off" class="text-search" placeholder="Type the City Name..." >
        <input type="button" class="button-search" value="Search">
    </div>

    <div class="main-container">
        <div>
            <div id="description"></div>
            <h1 id="temp"></h1>
            <div id="location"></div>
        </div>
    <div>
  <script src="main.js"></script>
</body>
</html>

这是我收到的 JSON 响应。

{
  "coord":{"lon":-74.01,"lat":40.71},
  "weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],
  "base":"stations",
  "main":{
    "temp":303.24,
    "feels_like":306.4,
    "temp_min":301.48,
    "temp_max":304.82,
    "pressure":1011,
    "humidity":74
  },
  "visibility":10000,
  "wind":{"speed":4.6,"deg":260},
  "clouds":{"all":1}, 
  "dt":1596415305,
  "sys":{
    "type":1,
    "id":4610,
    "country":"US",
    "sunrise":1596362046,
    "sunset":1596413419
  }
  "timezone":-14400,
  "id":5128581,
  "name":"New York",
  "cod":200
}

最佳答案

对于 5 天的天气预报,您需要调用 /forecast 而不是 /weather 端点。

例子:

https://api.openweathermap.org/data/2.5/forecast?q=New%20York&appid=<your-api-key>&cnt=5

关于javascript - openweathermap 上的 5 天天气预报未给出预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63222396/

相关文章:

javascript - 将 HTML5 拖放对象恢复到原始 div 容器

Javascript - 如何知道 Fetch API 中响应的类型?

android - 使用 OpenWeatherMap API key

javascript - OpenWeatherMap.ORG API - $.get JSON 不起作用,我没有收到任何数据

javascript - 使用 Office JS 遍历 Word 文档

javascript - 使用 parentElementArrayFinder 定位父元素

javascript - 你如何 stub 获取 api 请求

javascript - 无法从 Openweathermap API 获取温度

javascript - foreach 循环中的函数?

javascript - 如何使用 API 中的数据填充选择的下拉元素 - ReactJS