Reputation: 7059
I'm trying to extract the titles and descriptions from the MailChimp RSS feed using jQuery.
I'm trying with:
$.ajax({
type: 'GET',
url: 'http://us10.campaign-archive1.com/feed?u=21a65076da97205b5e5ff33e6&id=cc8bfc765e',
crossDomain: true,
dataType: 'jsonp',
success: function (xml) {
$(xml).find("item").each(function () {
var title = $(this).find("title").text();
var description = $(this).find("description").text();
var linkUrl = $(this).find("link_url").text();
var link = "<a href='" + linkUrl + "' target='_blank'>Read More<a>";
$('#feedContainer').append('<article><h3>' + title + '</h3><p>' + description + link + '</p>');
});
}
});
But I get the error:
Uncaught SyntaxError: Unexpected token < on
http://us10.campaign-archive1.com/feed?u=21a65076da97205b5e5ff33e6&id=cc8bfc765e&callback=jQuery214010618008393794298_1436280190025&_=1436280190026
If it's not possible through jQuery, is there another method? I tried with Yahoo Developer Console but robots.txt disallows the access..
It seems mailchimp disallow access that doesn't come from browsers, I tried to curl the URL and I get a 404 not found.
Upvotes: 12
Views: 466
Reputation: 17411
Uncaught SyntaxError: Unexpected token < on http://us10.campaign-archive1.com/feed?u=21a65076da97205b5e5ff33e6&id=cc8bfc765e&callback=jQuery214010618008393794298_1436280190025&_=1436280190026
The above error is caused by the fact that JSONP requires the returned data to be valid JSON/javascript, which XML is not.
Unfortunately JSONP is needed in your case as CORS isn't enabled on this specific server. (see CORS for Apache)
The result is that you cannot use jQuery only if the returned data can be changed to JSON.
I just tried curl
, and it seems to work fine, no 404 error for me, maybe you need to use a proxy? And make sure your quote the url, as the character &
is handled specially in a shell.
curl 'http://us10.campaign-archive1.com/feed?u=21a65076da97205b5e5ff33e6&id=cc8bfc765e'
And if you're going to use curl, there would be more than one options to parse xml, such as xmllint, see How to parse XML using shellscript?
Here's an example using xmllint
:
xmllint --nocdata --xpath '//item[1]/title/text()' <(curl -s \
'http://us10.campaign-archive1.com/feed?u=21a65076da97205b5e5ff33e6&id=cc8bfc765e')
It prints the first title:
AVVIO ANNO SCOLASTICO 2015/2016
Upvotes: 2
Reputation: 1438
You need to parse XML before query with jquery (https://api.jquery.com/jQuery.parseXML)
var xmlDoc = $.parseXML(xml);
var $xml = $(xmlDoc);
$xml.find("item");
Set valid dataType to xml
Upvotes: 2