使用jquery访问rss feed中的URL

时间:2014-11-17 15:14:15

标签: jquery ajax rss

我正在使用rss Feed并尝试创建一个模板来显示它的值。在这个阶段,我只是想确保我可以访问每个值。目前我在使用时记录了空字符串:

  $(function() { 
      $.ajax({
        url:'clnews.xml',
        dataType:'text',
        type:'GET',
        success:function(xml) {
           $xml = $(xml),
           $item = $xml.find( "item" );
           $.each($item, function(index, value){
           $value = $(value);
             $title = $value.find( "title" ).text();
             $link = $value.find( "link" ).text();
             $description = $value.find( "description" ).text();
            console.log($link);
           });
        },
        error: function (xhr, ajaxOptions, thrownError) {
        console.log(xhr.status);
        console.log(thrownError);
      }

  });
});

每个项目的xml如下所示:

 <item>
   <title>Antivirus Upgrades</title>
   <link>/backoffice/MyPractice/Education/News/MyPractice_AntivirusUpgrades1113.asp</link> 
   <description>Starting November 14, we will be upgrading your antivirus software to maximize protection from viruses. </description>
   <enclosure url="/backoffice/gif/news/InfoSec_th.jpg" length="0" type="image/jpg" articleUrl="/backoffice/gif/news/InfoSec_lg.jpg" tabletLg="/backoffice/gif/osi/InfoSec_lg.jpg" />
   <guid categoryId="8">68966</guid>
   <pubDate archiveDate="">Thursday, November 13, 2014 15:21</pubDate>
</item>

我怀疑问题是链接包含反斜杠。但我不确定如何最好地解决这个问题。我将无法更改rss的格式。提前谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

    .......
    success:function(xml) {
       $xml = $.parseXML( xml ); <<<=====
       $xml = $(xml),
       $item = $xml.find( "item" );
       .......

或者更好的是:

    .....
    dataType:'xml', //<<<<==== HERE
    type:'GET',
    success:function(xml) {
       $xml = $(xml),
       $item = $xml.find( "item" );
       ........