views:

272

answers:

2
+1  Q: 

JQuery Plugin Help

I'm trying to develop a JQuery plug-in that will access an outside XML feed and display the results. Here is what I have so far:

HTML Header Include

<script language="javascript" src="jquery.rss.js" type="text/javascript"></script>

JQuery Ready

<script type="text/javascript">
  $(document).ready(function() {  
    $("#rss").rss({count:6,loading_text:"loading"});
  });
</script>

Plugin (jquery.rss.js)

(function($) {

    $.fn.rss = function (o) {
     var s = {
      count: 6,
      loading_text: null,
     };

     if(o) $.extend(s,o);
     return this.each (function () {
      var list = $('<ul class="rss">').appendTo(this);
      var loading = $('<p class="desc"><center><img src="loading.gif" height="19" width="18" border="0"><br>'+s.loading_text+'</center></p>');
      var items = 0;
      var url = 'http://www.example.com/feed.xml;
      if (s.loading_text) $(this).append(loading);

      $.get(url,{},function(data){
       if (s.loading_text) loading.remove();  
       $('forecastday',data).each(function(i){
        var title = $(this).find("title").text();
        var description = $(this).find("description").text();

        list.append('<li>' + title + ' - ' + description + '</li>');

        items++;
        if(items == s.count) last;
       });
      });
     });
    }

})(jQuery);

Everything appears to be working correctly up until I try to do the $.get at which point nothing appears to be returned. I've verified by using alert() that the correct URL is being called from the $.get request.

Hopefully I'm not far off and a JQuery guru can point out where I'm going wrong. Thanks in advance for your help!

+1  A: 

You can't do ajax requests cross-domain. Either develop a server-side proxy (deployed on the same host) that routes your request to wunderground or look for API that supports JSONP.

See also - API to get weather based on longitude and latitude coordinates

Chetan Sastry
Then how do people pull RSS feed URLs and display them inline using JavaScript? Isn't accessing the XML file for the RSS feed a cross-domain request?
Russell C.
Do you have examples of these sites? I'm sure they use one of the above techniques. Here is an article that explains it in detail - http://developer.yahoo.com/javascript/howto-proxy.html
Chetan Sastry
You are correct. I created a proxy using mod_rewrite and it seems to have done the trip. Thanks for pointing me in the right direction!
Russell C.
+1  A: 

Check out this jQuery plugin: jdigiclock. It uses proxy to get and parse the XML data and serves it to the jQuery script. I think, that it is the same thing, you are looking for.

Dichev