如何通过Javascript检索跨域RSS(xml)

发布于 2024-11-27 13:04:28 字数 1616 浏览 0 评论 0原文

我计划在 Androind 应用程序中显示 Rss feed

我有这个 Rss feed url http://yofreesamples.com/category/free-coupons/feed/ 我无法控制或访问 RSS 源。

JSONP 是使用 JSON 输出响应的请求的解决方案。但在这里,我有 RSS 提要,可以用纯 xml 进行响应。

我有一个限制,不能使用代理来检索 rss 提要。我尝试使用 Google AJAX Feed API 实施,但遇到了问题。我需要获取entry_title的值 它位于回调函数内,并在我的其他函数中使用它,该函数在 android 应用程序内显示系统通知,但我无法获取该值,并且我不想使用任何容器并将其显示在 div 内。 是否有办法获取此值,或者是否有针对此问题的仅限客户端的解决方法

/* ---------------------- global variables ---------------------- */
var entry;
/* ---------------------- end global variables ---------------------- */
    function getRss(url){ 

    if(url == null) return false;

    google.load("feeds", "1");

    // Our callback function, for when a feed is loaded.
    function feedLoaded(result) {
        if (!result.error) {
            // Check out the result object for a list of properties returned in each entry.
            // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON

            var entry = result.feed.entries[0];             
            var entry_title = entry.title;
        }
        entry = entry_title; // not working

    }


    function Load() {
        // Create a feed instance that will grab feed.
        var feed = new google.feeds.Feed(url);
        // Calling load sends the request off.  It requires a callback function.
        feed.load(feedLoaded);

    }

    google.setOnLoadCallback(Load);             
}

I am planing to display Rss feed inside an Androind app

I have this Rss feed url http://yofreesamples.com/category/free-coupons/feed/
I do not have control or access to the RSS feeds.

JSONP is the solution for requests that respond with JSON output. But here, I have RSS feeds which can respond with pure xml .

I have a constraint of not using a proxy to retrieve rss feeds. I tried to implement with Google AJAX Feed API but I ran into a problem. I need to get the value of entry_title
which is inside the call back function and use it in my other function which displays a system notification inside the android app , but I am unable to get the value and I dont want to use any containers and display it inside a div. Is there a way to get this value or Is there a client-only workaround for this problem

/* ---------------------- global variables ---------------------- */
var entry;
/* ---------------------- end global variables ---------------------- */
    function getRss(url){ 

    if(url == null) return false;

    google.load("feeds", "1");

    // Our callback function, for when a feed is loaded.
    function feedLoaded(result) {
        if (!result.error) {
            // Check out the result object for a list of properties returned in each entry.
            // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON

            var entry = result.feed.entries[0];             
            var entry_title = entry.title;
        }
        entry = entry_title; // not working

    }


    function Load() {
        // Create a feed instance that will grab feed.
        var feed = new google.feeds.Feed(url);
        // Calling load sends the request off.  It requires a callback function.
        feed.load(feedLoaded);

    }

    google.setOnLoadCallback(Load);             
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惟欲睡 2024-12-04 13:04:28

我稍微修改了你的代码并且它有效

/* ---------------------- global variables ---------------------- */
var entry;
/* ---------------------- end global variables ---------------------- */
function getRss(url){ 

    if(url == null) return false;

    google.load("feeds", "1");

    // Our callback function, for when a feed is loaded.
    function feedLoaded(result) {
        if (!result.error) {
            // Check out the result object for a list of properties returned in each entry.
            // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON

            entry = result.feed.entries[0];             
        }
        // pass it to other function
        someFunction(entry.title);
    }

    function Load() {
        // Create a feed instance that will grab feed.
        var feed = new google.feeds.Feed(url);
        // Calling load sends the request off.  It requires a callback function.
        feed.load(feedLoaded);
    }

    // some function
    function someFunction(s) {
        alert(s);
    }

    google.setOnLoadCallback(Load);             
}

// calling it ?
getRss("http://yofreesamples.com/category/free-coupons/feed/");

I modified your code a little and it works

/* ---------------------- global variables ---------------------- */
var entry;
/* ---------------------- end global variables ---------------------- */
function getRss(url){ 

    if(url == null) return false;

    google.load("feeds", "1");

    // Our callback function, for when a feed is loaded.
    function feedLoaded(result) {
        if (!result.error) {
            // Check out the result object for a list of properties returned in each entry.
            // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON

            entry = result.feed.entries[0];             
        }
        // pass it to other function
        someFunction(entry.title);
    }

    function Load() {
        // Create a feed instance that will grab feed.
        var feed = new google.feeds.Feed(url);
        // Calling load sends the request off.  It requires a callback function.
        feed.load(feedLoaded);
    }

    // some function
    function someFunction(s) {
        alert(s);
    }

    google.setOnLoadCallback(Load);             
}

// calling it ?
getRss("http://yofreesamples.com/category/free-coupons/feed/");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文