【发布时间】:2016-07-15 16:44:02
【问题描述】:
过去一周我一直在努力了解 OpenWeatherMap API (link),但我目前无法将温度输入 JavaScript 变量。我使用过很多教程,但我还没有找到将温度值放入 JavaScript 变量的教程。
如果你们想知道,这就是 API 的结构:
http://api.openweathermap.org/data/2.5/weather?q=London&APPID=YourAPIKEY&units=metric
我目前有这个代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Weather App</title>
<script src="weatherapp.js" type="text/javascript"></script>
<style> body {padding: 0; margin: 0;} canvas {vertical-align: top;} </style>
</head>
<body>
<script>
var weather;
loadJSON ('http://api.openweathermap.org/data/2.5/weather?q=London&APPID=YOURDATA', gotData);
function gotData(data) {
weather = data;
window.alert(weather.main.temperature);
}
</script>
</body>
</html>
提前致谢, 瑞奇
【问题讨论】:
-
你的 window.alert 函数——它有效吗?你看到那里有温度吗?如果是这样,您可以分配一个变量,例如
var temperature = weather.main.temperature; -
嘿@GridTrekkor,感谢您的回复。我试过了,它仍然没有用。我认为这可能是
loadJSON ('http://api.openweathermap.org/data/2.5/weather?q=London&APPID=YOURDATA', gotData);的问题? -
删除 YOURDATA 并替换为您的 API 密钥。
-
@GridTrekkor,我只是不想让任何人知道我的 API 密钥。
-
您的提醒功能正常吗?
标签: javascript html json openweathermap