jQuery.get(): Trying to retrieve feed, but XML tags are lost
I'm using jQuery to automatically fetch the most recent post on a blog. jQuery.get() goes to the blog's RSS feed and returns the most recent post:
Here's what I'm using:$.get('http://url.to/feed', function(feed) {data = $(feed).find('item:first').text(); $('#testbox').html(data);}, 'xml');
Here's the problem: That spits out just the text from the feed and the HTML elements. All the stuff like <title>Title</title
is just stripped to Title
. I need to keep all those XML elements intact so I can style things properly.
I've also tried .html()
instead of .text()
, but that doesn't work. Using neither one also doesn't work (i.e. data = $(f开发者_如何学Ceed).find('item:first');
).
How can I download and display a section of an RSS feed and not strip the XML tags?
Thanks!Hum... Take a look if your content can't just be wrapped with <![CDATA[
and copied.
See this answer (I know, CDATA abuse and everything, but OP don't want to parse the XML at all, just append it directly to the DOM).
Update: What actually worked
Change your call to the get
method so it interprets the response as pure text and let JQuery create the DOM Tree for you. Have a look here.
If you append it, it works.
$(feed).find('item:first > *').appendTo('#testbox');
Try returning the node, and not the text...
$.get('http://url.to/feed', function(feed) {
data = $(feed).find('item:first');
$('#testbox').html(data);
}, 'xml');
How about trying something like this (It worked for me)...
Create a PHP file (get_xml.php) with the following:
$xml = file_get_contents($_GET['url']);
header("Content-Type: text/xml");
echo $xml;
Then use the following jQuery:
$.get("get_xml.php",{url:'http://url.to/feed/'}, function(data){
var data = $(data).find('item:first');
$('#testbox').html(data);
});
精彩评论