【发布时间】:2015-10-12 05:27:40
【问题描述】:
我正在为我的网站开发本地天气小部件...
我已使用OpenWeather Api 成功显示数据
因为我在 jquery/javascript 方面没有足够的经验,所以我在 10 天内无法加载数据......
我该怎么做,请一些帮助....任何帮助或参考将不胜感激...
HTML 和脚本
<div id="fatehjang"></div>
<script type="text/javascript">
function getWeather(coords, callback) {
var url = 'http://api.openweathermap.org/data/2.5/forecast/daily?lat=33.568109&lon=72.642767&cnt=7';
$.ajax({
dataType: "jsonp",
url: url,
jsonCallback: 'jsonp',
data: { lat: coords[0], lon: coords[1] },
cache: false,
success: function (data) {
callback(data);
}
});
}
$(document).ready(function () {
var teams;
$.getJSON("http://api.openweathermap.org/data/2.5/forecast/daily?lat=33.568109&lon=72.642767&cnt=7", function (json) {
//do some thing with json or assign global variable to incoming json.
teams = json;
});
$(window).load(function () {
for (var team in teams) {
var obj = teams[team];
(function (team) {
coords = [team.Lat, team.Long]
getWeather(coords, function (data) {
var html = [];
html.push('<h2 class="heading-md">')
html.push('Maximum Humidity: ', data.main.humidity, ', ');
html.push('</h2>')
$("#fatehjang").replaceWith(html.join('')).css("background-color", "white");
});
}(obj));
}
});
});
</script>
**Html**
感谢您的宝贵时间
【问题讨论】:
-
我在您的请求中看到您正在使用
cnt参数,我相信这意味着count天。现在你得到了 7 天的天气,所以cnt的值是7。服用 10 天 - 在您的网址中将其更改为10: `api.openweathermap.org/data/2.5/forecast/… 这是否回答了您的问题 - “我在 10 天内无法加载数据......”?或者你有什么问题?形容它。不仅在标题中,而且在问题的内容中也这样做。看起来您在标题中有一个问题,而在问题正文中有另一个问题
标签: javascript jquery json api asp.net-web-api