简体   繁体   English

使用jQuery从ajax / jsonp请求输出html

[英]Output html from ajax/jsonp request using jQuery

I'm trying to output html using an ajax request from a jsonp file . 我正在尝试使用来自jsonp文件的ajax请求输出html。 When using console.log I'm seeing the data looping fine, however when using a variable to grab the loop and output in html, I'm only seeing one result. 使用console.log我看到数据循环良好,但是使用变量捕获循环并以html输出时,我只看到一个结果。 What am I missing? 我想念什么?

$.ajax({
    url: "http://sitedotcom/blog/json",
    dataType: "jsonp",
    jsonpCallback: "jsonpCallback",
    success: jsonpCallback
});

function jsonpCallback(data) {
    for (var key in data) {
        if (data.hasOwnProperty(key)) {
            console.log(data[key]["title"] + ", " + data[key]["entry_id"]);
            rssData = '<h2>' + data[key]["title"] + "</h2><p>" + data[key]["blog_summary"] + "</p>";

        }
        $('#blog-content').html(rssData);
    }
}

You have $('#blog-content').html(rssData); 您有$('#blog-content').html(rssData); inside the loop....so only the last result will show since html() replaces all content. 在循环内...。因此只有最后一个结果会显示,因为html()替换了所有内容。

Try using append() instead or concatenate rssData each iteration and set html() after loop completes 尝试使用append()代替,或rssData每次迭代中连接rssData并在循环完成后设置html()

It looks like rssData is out of scope when you are adding it to your #blog-content. 将rssData添加到#blog-content时,它似乎超出范围。 Also you are outputting different data when doing your console log and setting your rssData variable. 同样,在执行控制台日志和设置rssData变量时,您将输出不同的数据。

If you could provide the json layout and also the html layout you are trying to append to. 如果您可以提供json布局,也可以提供要尝试附加的html布局。

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

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