简体   繁体   English

使用jQuery的嵌套JSON提取

[英]Nested JSON fetch using jQuery

I am trying to create an RSS Feed kind of Message display from Yammer. 我正在尝试从Yammer创建一种RSS Feed类型的消息显示。

<script type="text/javascript">
    var cleanit = null;
    $(document).ready(function(){ cleanit = setInterval('callYammer()', 50);});
    function callYammer(){ 
        clearInterval(cleanit);
        $.getJSON("./yammer.feed?request=messages",function(json) {
            var objYammer = $("#yammerFeed");
            objYammer.html('');
            $.each(json.messages, function(i, m) {
                if(!m.replied_to_id && m.body.plain){
                    var data = "<li>" + m.body.plain;
                    $.getJSON("./yammer.feed?request=users&userid="+m.sender_id,function(jsonUser) {
                        //alert(jsonUser.full_name);
                        data = data + " - "+jsonUser.full_name;
                    });
                    data = data + "</li>";
                    objYammer.append(data);
                }
            });
        });
        return false;
    }
</script>

I want to display Message along with it's Username. 我想显示消息及其用户名。 But in the end, from firebug debugger, what I see is the inner JSON data is not getting appended as I expected. 但是最后,从firebug调试器中,我看到的是内部JSON数据没有按我期望的那样追加。 Though the calls are hitting and data is coming from the call, the 尽管通话打到并且数据来自通话,但是

data = " - "+jsonUser.full_name; data =“-” + jsonUser.full_name;

is getting executed after all JSON calls for Users. 在对用户的所有JSON调用之后被执行。

How do I append Username from inner JSON call to main JSON data? 如何将用户名从内部JSON调用追加到主要JSON数据?

You call the lines 你打线

data = data + "</li>";
objYammer.append(data);

in the code following your inner getJSON AJAX call, but that probably results in these lines being executed before the AJAX request has finished. 在内部getJSON AJAX调用之后的代码中,但这可能导致这些行在AJAX请求完成之前执行。 Put the code INTO the inner AJAX success function to make sure it is fired only after the result is available. 将代码放入内部AJAX成功函数中,以确保仅在结果可用后才将其触发。

function callYammer(){ 
    clearInterval(cleanit);
    $.getJSON("./yammer.feed?request=messages",function(json) {
        var objYammer = $("#yammerFeed");
        objYammer.html('');
        $.each(json.messages, function(i, m) {
            if(!m.replied_to_id && m.body.plain){
                var data = "<li>" + m.body.plain;
                $.getJSON("./yammer.feed?request=users&userid="+m.sender_id,function(jsonUser) {
                    console.log('1:'+jsonUser.full_name);
                    data += " - "+jsonUser.full_name + "</li>";
                    objYammer.append(data);
                    console.log('2:'+data);
                });
            }
        });
    });

Edit: 编辑:

Just added the console.log() statements. 刚刚添加了console.log()语句。 What do they return? 他们返回什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM