Search code examples
jqueryjquery-pluginsweatherweather-api

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!


Solution

  • 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