javascript - 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;
});
});
解决方案
仅从快速概述来看,您似乎使用了错误的引号。
尝试将您的 API 变量放入``
而不是''
.
推荐阅读
- python - 三元 if-else 语句中的语法错误
- macos - 如何在 mac 上为另一个驱动器设置路径变量
- python - python:NameError:未定义全局名称“conn”| 插座 |
- android - 使用 FCM 的 Android 8.1 Oreo 无法推送通知
- c++ - 在编译时确定参数的唯一性
- javascript - 在 React Redux 中导航回浏览器中的上一页后,状态未按预期工作
- vba - 文本框的VBA excel问题
- angular - 验证消息显示在“模糊”但不提交
- c# - 如何在组合框中的datatrigger中获取项目源的值?
- sql-server - 找不到 SQL Server bak 文件