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

时间:2022-08-23 10:02:35

I am planing to display Rss feed inside an Androind app

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

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

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

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

JSONP是使用JSON输出响应的请求的解决方案。但是在这里,我有RSS源,它可以用纯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

我有一个不使用代理来检索rss feed的约束。我尝试使用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);             
}

1 个解决方案

#1


2  

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/");

#1


2  

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/");