首页 > 解决方案 > JavaScript 无法访问天气 API

问题描述

我只想编写一个简单的天气应用程序来学习 JS。搜索了整个网络后,我找不到错误...

我没有从 API 获得任何信息。我在浏览器控制台中检查了这一点。

我只想展示洛杉矶当前的天气状况。

window.addEventListener("load", () => {
  let temperatureDescription = document.querySelector(".temperature-description");
  let temperatureDegree = document.querySelector(".temperature-degree");
  let locationTimezone = document.querySelector(".location-timezone");

  const proxy = "https://cors-anywhere.herokuapp.com/";
  const api = '${proxy}https://api.darksky.net/forecast/4d5ddfbfc8eabb0dddcb9e78fb408a99/37.8267,-122.4233';

  fetch(api)
    .then(response => {
      return response.json();
    })
    .then(data => {
      console.log(data);
      const {
        temperature,
        summary
      } = data.currently;

      temperatureDegree.textContent = temperature;
      temperatureDescription.textContent = summary;
      locationTimezone.textContent = data.timezone;
    });
});

标签: javascript

解决方案


仅从快速概述来看,您似乎使用了错误的引号。

尝试将您的 API 变量放入``而不是''.


推荐阅读