如何使用JSON检索OpenWeatherMap数据
How to retrieve OpenWeatherMap data using JSON?
我一直在尝试用JavaScript编写一个程序,该程序可以使用JSON从OpenWeatherMap检索天气数据。我对JSON很陌生,但我认为我理解它背后的逻辑。然而,当我点击"获取JSON"按钮时,什么都没有发生。getJSON函数中的"data.temp"可能是不正确的,但如果我理解正确,它似乎至少应该打印"Temperature"一词:HTML和JavaScript附在下面,如果有任何帮助,我们将不胜感激。
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script>
<div id = "owmData" style = "background-color:#cc0;">
Weather data should go here.
</div>
获取JSON
JavaScript:
$(document).ready(function() {
/* Operate when "getIt" button is clicked.*/
$("#getIt").click(function(event){
/* Variable storing weather information.*/
var weatherNow="http://api.openweathermap.org/data/2.5/weather?q=London,uk&callback=test&appid=******************";
$.getJSON(weatherNow,function(data){
$('#owmdata').append('<p>Temperature : ' + data.temp+ '</p>');
});
});
});
在请求参数中,您将"test"指定为回调函数。你可以这样构建你的请求url,而不需要对回调的引用,并直接访问数据:
http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=*************
"temp"嵌套在属性"main"中,因此您可以将其引用为data.main.temp
相关文章:
- 如何在 PHP var OpenWeatherMap 中获取第二行 js 数据
- 如何使用javascript在网络上仅将UTC时间戳转换为本地时间?从 http://openweathermap.or
- 我有来自OpenWeatherMap API的风向数据,数据以0到360度表示.我想将其转换为文本格式
- 如何使用JSON检索OpenWeatherMap数据
- 包括使用Javascript的OpenWeatherMap API
- 天气预报,使用angularjs和openweathermap将数据从旧数组推送到新数组中
- OpenWeatherMap地图只显示莫斯科
- openweathermap api在IE11中不工作
- OpenWeatherMap API HTTPS refusal javascript
- 如何使用OpenWeatherMap API的Javascript
- OpenWeatherMap.Org API - $.getJSON不起作用,我也没有接收到任何数据
- 不能't从openWeatherMap api JavaScript获取数据
- 使用OpenWeatherMap-net::ERR_CONNECTION_REFUSED进行jQuery ajax调用
- 从 OpenWeatherMap.org 获取天气数据并放入Javascript值