开发者

blogger jquery call

开发者 https://www.devze.com 2022-12-08 03:58 出处:网络
I have a blog on google (blogger) and i want to get json data from external website in order to integrate into my posts.

I have a blog on google (blogger) and i want to get json data from external website in order to integrate into my posts. I used jquery library and getJson function in order to retrieve the json data but n开发者_运维技巧othing is returned.

Blogger restrict any jquery external calls ? Have any idea? Thank you


You can, check out http://code.google.com/apis/gdata/docs/json.html for information. But, look at the JSONP mode used by jquery and you'll see how to do it. For example, the following code shows how to get a comment feed from the Blogger API:

    var BloggerImporter = {

    getComments : function(username){

        var feedURL = "http://"+username+".blogspot.com/feeds/comments/default";

        var paras = {
            alt : 'json-in-script'
        };

        $.ajax({
            url: feedURL,
            type: 'get',
            dataType: "jsonp",
            success: BloggerImporter.onGotCommentData,
            data: paras
        });


    },

    /**
     * Parse the JSON comment data returned by the Google Blogger API
     */
    onGotCommentData : function(data){

        var feed = data.feed;
        var entries = feed.entry || [];
        var txt = "";

        for (var i = 0; i < entries.length; ++i) {
            var entry = entries[i];
            var title = entry.title.$t;
            txt += title;
        }

        alert(txt);
    }
}


Here is the code I use to inline my entire blog. Not good for SEO as google will never see it, but all I really cared about was making the blog page look half decent.

<div id="blogContainer"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.ajax({
    url: 'http://www.blogger.com/feeds/<your blogger ID>/posts/default?alt=json-in-script',
    type: 'get',
    dataType: "jsonp",
    success: function(data){
        for (var i = 0; i < data.feed.entry.length; i++){
            $('#blogContainer').append('<div class="blogItem"><h2>' 
                   + data.feed.entry[i].title.$t + '<span class="author"> by ' 
                   + data.feed.entry[i].author[0].email.$t + '</span></h2>' 
                   + data.feed.entry[i].content.$t 
                   + '<br clear="all"></div>');
        }
    }
});
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消