2011-10-20 61 views
0

我發現this site允許將RSS訂閱源轉換爲json。 它還提供了一種指定回調的方法,所以我認爲用戶可以對此Web服務進行jsonp調用。 但是,我嘗試了不同的方式來做到這一點,但沒有工作。 這裏是我的代碼:使用jquery和ajax獲取RSS訂閱

$(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) 
     } 
    }); 
}); 

無論我試試,成功處理程序沒有得到執行。我得到錯誤處理程序。 我試着用jsonpCallbak:「回調」,jsonpCallback:「?」,param:「回調」等其他值也沒有成功。 我必須使用沒有支持任何服務器端腳本語言(沒有APS,沒有PHP等)只有JavaScript 有人得到這項服務在他的網站工作? 任何建議將非常感謝!

回答

1

更新:

下面是爲您的代碼工作的例子:

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

問題是,我得到與返回的JSON一些JavaScript錯誤:

看到這個jsfiddle

+0

你看了我的問題嗎? 我寫了我不能使用PHP,所以我不能使用示例提供的基本代理。 – andreapier

+0

對不起更新了我的文章 –

+0

謝謝,我會聯繫網站管理員並要求修復xml-to-json-轉換器 – andreapier

3

我發現jQuery JSON API不適合這種提供BlastCas的JSON響應ta服務。它將JSON分配給URL中指定的自定義變量,並且不使用JSONP使用的回調函數。比如這個網址: http://www.blastcasta.com/feed-to-json.aspx?feedUrl=http%3A//xml.corriereobjects.it/rss/homepage.xml&param=rssFeed將返回如下回應:

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

因此,可以使用腳本注入工藝:

/* 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; 
}); 
+0

那麼,從技術上說,BlastCasta不會返回JSON響應。它返回JS腳本。 –