Posts Tagged ‘cross-domain’

10Feb

So I have been struggling for the past month trying to get a Perl CGI script that acts as a proxy to work on certain servers. The client only supports/allowed perl (as far as i knew) and didn’t allow it to output data to a browser for some reason. So you can see my dilemma. I tried various means. Worked with their IT team, etc… I kept trying to find alternative methods. I knew JSONP was an option but it’s not like RSS feeds are in JSON or are coming from a JSONP server. Finally someone told me about YQL. In short, I am pretty impressed. I essentially used it as a proxy and XML -> JSON converter. The script in JQuery is really short and goes like this:

var maxEntries = 5; // if 0 then there will be no limit

function slug(str){
 return str.replace(/\s/g,'-').replace(/[^a-zA-Z0-9\-]/g,'').toLowerCase();
}
/**
 * This function appends rss feed items to divs with the same slug
 * Name is the title of the feed, when slugged i use it as a div id
 * File is the rss feed
 */
function newFeed(name, file) {
 $.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%22"+encodeURIComponent(file)+"%22&format=json&callback=?", function(d) {
  var count = 0;
  //grab ever rss item from the json result request
  $(d.query.results.rss.channel.item).each(function() {
    //if set up to be infinite or the limit is not reached, keep grabbing items
    if(maxEntries == 0 || maxEntries>count){
     var title = this.title;
     var link = this.link;
     var description = this.description;
     var pubDate = this.pubDate;
     // Format however you want, I only went for link and title
     var anItem = "<a href='"+link+"' target='_blank'>"+title+"</a><br>";
     //append to the div
     $("#"+slug(name)).append(anItem);
     count++;
    }
   });
 });
};

I hope you guys find this useful. It made my life a ton easier to deal with. Coming soon… I’ll show you how to convert Andrew Sellick’s Sexy Slide Menu for Prototype to JQuery! WOO!

Share