简体   繁体   English

使用jquery和ajax获取RSS feed

[英]Getting rss feed with jquery and ajax

I found this site that allows to convert RSS feeds into json. 我发现这个网站可以将RSS资讯提供转换成json。 It also provides a way to specify a callback, so i think users are able to make jsonp calls to this web service. 它还提供了一种指定回调的方法,因此我认为用户可以对此Web服务进行jsonp调用。 However, i tried different ways to do that but none worked. 但是,我尝试了不同的方法来做到这一点,但是没有一个奏效。 Here is my code: 这是我的代码:

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: 'http://www.blastcasta.com/feed-to-json.aspx',
        dataType: "jsonp",
        jsonpCallback: "loadRSS",
        data: {
            feedUrl: 'http://xml.corriereobjects.it/rss/homepage.xml',
            param: "callback"
        },
        success: function (data) {
           var list = "";
           for (var propertyName in data) {
                list+=data[propertyName];
            }
            console.log(list);
        },
        error: function(xhr, ajaxOptions, thrownError){
            alert(ajaxOptions)
        }
    });
});

Whatever i try, the success handler doesn't get executed. 无论我尝试什么,成功处理程序都不会执行。 I get error handler instead. 我得到错误处理程序。 I tried with jsonpCallbak: "callback", jsonpCallback: "?", param: "callback" and other values too but without success. 我尝试使用jsonpCallbak:“回调”,jsonpCallback:“?”,param:“ callback”和其他值,但均未成功。 I have to use ONLY javascript without the support any server side scripting language (no aps, no php, etc.) Did someone get this service working in his site? 我只需要使用不支持任何服务器端脚本语言的JavaScript(无aps,无php等),有人在他的网站上获得了此服务吗? Any suggestion would be really appreciated! 任何建议将不胜感激!

I find jQuery JSON API not suitable for this kind of JSON response that provides BlastCasta service. 我发现jQuery JSON API不适合提供BlastCasta服务的这种JSON响应。 It assigns JSON to a custom variable, specified in URL, and doesn't uses callback functionality JSONP operates with. 它将JSON分配给在URL中指定的自定义变量,并且不使用JSONP配合使用的回调功能。 For example this URL: http://www.blastcasta.com/feed-to-json.aspx?feedUrl=http%3A//xml.corriereobjects.it/rss/homepage.xml&param=rssFeed will return following response: 例如,此URL: http : //www.blastcasta.com/feed-to-json.aspx? feedUrl= http% 3A//xml.corriereobjects.it/rss/ homepage.xml& param= rssFeed将返回以下响应:

rssFeed = { "rss": { "channel": /*...*/}}

So, script injection technic may be used: 因此,可以使用脚本注入技术:

/* URL of the BlastCasta service and his parameters:
  feedUrl :== escaped URL of interest (RSS Feed service)
  param   :== javascript variable name which will receive parsed JSON object */
var url = "http://www.blastcasta.com/feed-to-json.aspx"
  +"?feedUrl=http%3A//xml.corriereobjects.it/rss/homepage.xml"
  +"&param=rssFeed";

/* since the service declares variable without var keyword,
   hence in global scope, lets make variable usage via window object;
   although you can write param=var%20rssFeed" in the URL :) */
window.rssFeed = null;

$.getScript(url, function() {
  /* script is loaded, evaluated and variable is ready to use */
  console.dir(window.rssFeed);

  /* some feeds are huge, so free the memory */
  window.rssFeed = null;
});

Update: 更新:

here's an example that works for your code: 这是一个适用于您的代码的示例:

$.getJSON("http://www.blastcasta.com/feed-to-json.aspx?feedUrl=http://xml.corriereobjects.it/rss/homepage.xml&param=?", function(data) {
    console.dir(data);
});

problem is, that I get some javascript errors with returning json: 问题是,我在返回json时遇到了一些JavaScript错误:

see this jsfiddle 看到这个jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM