【问题标题】:how to set json data through for loop如何通过for循环设置json数据
【发布时间】: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


【解决方案1】:

改变

                teams = json;
            });
            $(window).load(function () {
                for (var team in teams) {

                teams = json;
                for (var team in teams) {

上面的更改只是执行“显示”数据作为“成功”回调的一部分的代码。由于 getJSON 是异步的,唯一安全的地方是成功回调的一部分——我见过人们(我和他们一起工作)用 setTimeouts 等“伪造”它,他们认为他们已经成功了,直到服务器需要额外的时间来响应 - 然后一切都崩溃了:p

【讨论】:

  • 感谢您的回答,我一定会在测试+1后接受它
  • 请注意,我尚未检查您的代码的有效性 = 但是,我知道您想要实现的目标。 - 我在答案中做了一点解释
  • $("#fatehjang").replaceWith 建议您在 teams 的每次迭代中替换该元素中的 HTML - 也许您应该改用 $("#fatehjang").append
猜你喜欢
  • 1970-01-01
  • 2020-01-12
  • 2010-10-29
  • 2017-09-18
  • 1970-01-01
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多