views:

1625

answers:

4

I have the following Javascript code, where feed is a valid url to an rss feed.

jQuery(function() {

        jQuery.getFeed({
            url:feed,
            success: function(feed){
                      alert(feed);
                      }
        });
    });}

when i run this the alert never shows up. using firebug i can see the connection being made and the response from the feed.

Note: i know this is a security issue and some browsers block it, this isnt the case at hand because i have worked around that issue and the browser no longer blocks the connection

EDIT : for some reason i cant make the comment show up so here:

The call isnt being proxyed by a script at the moment, since this is a test im just overriding browser security settings with:

netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
A: 

I had a similar problem. Try hard coding the URL into the url: and see if that works rather than setting a feed variable.

You have an extra } at the end of your segment, is that just an extra part of your code?

Francis Gilbert
that doesnt solve the problem, and yes the extra } comes from previous code
Nuno Furtado
+3  A: 

With the information you give, we can see that your call is correct. The problem is probably somewhere else. In suggest that you dig in jFeed source code and disable feed parsing in $.ajax success method:

    $.ajax({
        type: 'GET',
        url: options.url,
        data: options.data,
        dataType: 'xml',
        success: function(xml) {
            var feed = xml; // <== was "var feed = new JFeed(xml);"
            if(jQuery.isFunction(options.success)) options.success(feed);
        }
    });

If your alert pops up, it's a feed parsing problem. In this case, you can check that the xml is correct and submit it here for further investigation.


I've run some tests and checked jQuery code. You said that you worked around the browser security problem: I guess that you installed on you server a proxy script that will download the rss file from the distant server and then transmit it to your ajax request (because the browser will block ajax calls to a server different from the one your page is on). When making an ajax call with dataType : 'xml', jQuery expects that the content type of the response to contain the string "xml":

var ct = xhr.getResponseHeader("content-type"),
  xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
  data = xml ? xhr.responseXML : xhr.responseText;

Here are my questions:

  • Did you use a script as proxy script as I suppose ?
  • Does this script set the content-type to something containing xml ?

Here is a minimalistic php sample shipped with jFeed:

<?php
header('Content-type: application/xml');
$handle = fopen($_REQUEST['url'], "r");

if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
    }
    fclose($handle);
}
?>
ybo
nothing happens, i've even added alerts to that function and nothing happens
Nuno Furtado
This means that the ajax call never succeeds, nothing to do with jFeed then ? Can you try to change the datatype to "text" ? (you don't need to use jQuery.getFeed, just call directly $.ajax)
ybo
if i change that datatype to text the ajax call succeeds and the alert shows up and when i rever the first change we did, i can now use the feed as it was intended. Could this be a problem in the rss feed?
Nuno Furtado
dataType : "text" tells jQuery to give you the data as is, it was just to check that everything with the ajax call. dataType : "xml" tells jQuery to process the result of the call as xml and it seems to fail. Can you post the result of the ajax call to make sure it's valid xml ?
ybo
http://blitz.aeiou.pt/blitz_feed.rss
Nuno Furtado
+1  A: 

Just to add that for those needing to get content out of feeds with customized namespaces, with jFeed, you could do stuff like so

<dc:creator>SomeBody Name</dc:creator>
<media:thumbnail url="someimage.jpg" />

Add your own items to JFeed Prototype

JFeedItem.prototype = {
        title: '',
        link: '',
        description: '',
        updated: '',
        id: '',
     mcontent:'',
     mdescr:'',
     mcredit: '',
     dcreator:'',
     mthumbnail:''
    };

Then to the prototype of JRss and JAtom, which ever your using, add the following the item loop

item.dcreator = $(this).find("dc\\:creator").eq(0).text();
item.mcontent = $(this).find("media\\:content").eq(0);
item.mdescr = $(this).find("media\\:description").eq(0).text();
item.mcredit = $(this).find("media\\:creator").eq(0).text();
item.mthumbnail = $( $(this).find("media\\:thumbnail").eq(0) ).attr("url");

Hope this one day helps someone too struggling with handling namespaces in xml with jQuery. I am working with RSSfeeds from the NYT and needed to get media thumbnails to display in links to video content, and had to spend some time to figure out how to go work thru namespaces

dr.stonyhills
A: 

one more thing if you want to read content from custom namespaces:

the above ('escaped colon') method won't work properly in Chrome but this ('nodeName') approach will work in all browsers:

item.mediaThumbnail = $($(this).find("[nodeName=media:thumbnail]").eq(0)).attr("url");

aleks