14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Feed APIを使おう

Last updated at Posted at 2014-02-22

JavaScriptでRSS/ATOMを取得してサイトに表示したり加工して使ったりしたいんだけど、
そのままAJAXでやろうとしても、いわゆるSame Origin Policy でドメインが違うサイトのデータは取得できない。
そんなときは、Google Feed APIを使おう。

使い方

以下を読もう

サンプル

http://ameblo.jp/nazuki-airu/ のRSSを取得して各記事のリンクを作成する
下記がそのサンプル

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("feeds", "1");
function initialize() {
    var feed = new google.feeds.Feed("http://feedblog.ameba.jp/rss/ameblo/nazuki-airu/rss20.xml");
    feed.setNumEntries(10);
    feed.load(function(result) {
        if (!result.error) {
            for (var i = 0; i < result.feed.entries.length; i++) {
                var entry = result.feed.entries[i];
                $("#ret").append(
                    $("<div>").append(
                        $("<a>").attr("href", entry.link).text(entry.title))
                );
            }
        }
    });
}
google.setOnLoadCallback(initialize);
</script>
</head>
<body>
<div id="ret"></div>
</body>
</html>

結果は以下のようになる。

rss_sample.png

14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?