簡體   English   中英

在頁面中顯示json結果

[英]Display json result in page

我有以下js腳本和html代碼,並想在頁面上輸出結果。 當我在控制台中運行它時,我可以看到如下結果:

[對象{title =“他們沒有鞠躬;他們沒有燃燒”,link =“ http://xxxxxxx.com/bow-burn/”,author =“ Admin”,更多...},對象{ title =“您最后一次滑稽的生活嗎?”,鏈接=“ http://xxxxxxx...significant-areas-life/”,author =“ Admin”,更多...},對象{title =“婚姻在所有方面都是光榮的…”,link =“ http://xxxxxxx...arriage-honourable-all/”,author =“ Admin”,更多...},對象{title =“住宅在“上帝的堡壘”中,鏈接=“ http://xxxxxxx...elling-strongholds-

html代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">

    <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
    Remove this if you use the .htaccess -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <script src="css/style.css"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
    <script src="js/script.js"></script>
</head>
<body>

    <div id="blog" data-role="page">
    <div data-role="header" class="sys_hd" data-position="fixed" data-id="sys_header" >
        <h1>Sysads Posts</h1>
        </div><!-- header -->
        <div data-theme="c" data-role="content" id="postlist"> </div><!-- content -->
        <div data-role="footer" data-position="fixed" data-id="sys_footer" >
                    <div data-role="navbar" >
                <ul>
                    <li><a href="#blog" class="sys_ft">Home</a></li>
                    <li><a href="#blog" class="sys_ft">Disclaimer</a></li>
                </ul>
            </div><!-- navbar --> 
        </div><!-- footer --> 
    </div><!-- page -->
</body>
</html>

js代碼:

$(document).ready((function(){
     url = 'http://xxxxxxxx.com/category/daily-devotion/feed/';
        $.ajax({
        type: "GET",
        url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
        dataType: 'json',
        error: function(){
            alert('Unable to load feed, Incorrect path or invalid feed');
        },
        success: function(xml){
            postlist = xml.responseData.feed.entries;
            console.log(postlist);
        }
    });
}));

json格式可能不正確。 http://json.parser.online.fr/說什么?

試試stringfy方法:

JSON.stringfy(obj)

嘗試:

for(var i in postlist) {
   var obj = postlist[i];
   console.log("title >>>  "+obj.title);
   console.log("link>>>  "+obj.link);
   ...........//other properties
}

從您的問題中尚不清楚您要完成什么,但是從標題和提供的代碼來看,我猜您想將檢索到的條目添加到頁面的“ #postlist”部分。 如果正確,則可以執行以下操作:

success: function(xml){
    postlist = xml.responseData.feed.entries;

    $.each(postlist, function(entry){

         $('#postlist').append($('<div class="entry">' + entry.title + '</div>'));

    });    

}

顯然,添加其他您喜歡的標記方式。

希望能有所幫助。

嘗試這個:

success: function(data ){
            var postlist = data.responseData.feed.entries;
            var html = '<ul data-role="listview" data-filter="true">' ;
            for (var i = 0 ; i < 10 ; i++) {
                var entry = postlist[i];
                html += '<li>';
                html += '<a href="' + entry.link + '">' ;
                html += '<div>' + entry.title + '</div>' ;
                html += '<div>' + entry.author + '</div>' ;
                html += '</a>';
                html += '</li>';
            }
            html += '</ul>';
           $( "#postlist" ).append(html);

        }});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM