Night | Snow

Ajax Request for RSS Feeds with jQuery

Posted on: November 28th, 2009

Tags: , ,

In this example I will show you how to easily load RSS Feeds into your web site by using Ajax. Of course you can load Rss Feeds directly in your Php script, but an Ajax request won’t slow down the loading process. However for security reasons, Ajax cannot make remote/external calls to XML. Therefore you have to create a bridge in Php, that accesses the Feeds and returns the XML locally. Anyways here’s how to get your Twitter feeds- but of course you can apply this code to any other XML file.

Here’s the bridge.php file:

<?php
header("Content-Type: application/xml; charset=UTF-8");
$feed = (isset($_REQUEST['feed'])) ? $_REQUEST['feed'] : '';
if(!empty($feed) || is_string($feed)) echo file_get_contents("http://".$feed);
?>

Ajax Request:

jQuery(document).ready(function() {  
    jQuery("#loading").show();  
    jQuery.ajax({  
        type: "GET",  
        url: "bridge.php?feed="+escape("twitter.com/statuses/user_timeline/55789790.rss"),  
        dataType: "xml",  
        success: parseXml  
    });  
    function parseXml(xml) {
        var items = xml.getElementsByTagName('item');
        $("lasttweet").set("html",items[0].getElementsByTagName('description')[0].firstChild.nodeValue);
    }  
});

Posted in jQuery | Trackback Url

News

Martin Fischer said:

Client and partner of simra.ch

Wir können Simon Schärer als kompetenter und zuverlässiger Programmierer höchst weiterempfehlen. Er ist nicht nur ein guter Programmierer, sondern auch ein ausgezeichneter Designer.
Danke Developer’s Island

Follow us on Twitter

favorite

I get asked a lot - which SDK should we use for web programming? I recommend Netbeans

my toolbar

Simra GameMP3 playersubscribe to RSSMy delicious bookmarksFacebookTwitterE-mail

No Responses to “Ajax Request for RSS Feeds with jQuery”

RSS Feed Icon  Subscribe to comments follow-up

Trackbacks/Pingbacks

Leave a reply

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Latest Tweet

loading...