Search code examples
ajaxcross-domainjsonpfeedatom-feed

Atom to JSONP conversion woes


So, I'm developing a news feed that exists on another server along with the static PDF documents that the feed describes.

I'm trying to implement client-side-only feed parsing because I'm not sure what the back-end server capabilities will be.

As a shortcut workaround, I'm parsing the Atom feed to JSON using Blastcasta.com

url = "http://www.blastcasta.com/feed-to-json.aspx?feedurl=http://[atomLocation]/newsletter.atom"

data = {}
$.ajax({
  url: url + "?callback=?",
  dataType: "jsonp",
  data: data,
  success: function(data) { onSuccess(data); },
  error: function() { alert('Failed to parse feed'); },
});

If I set the dataType to 'application/json' I get a cross origin error. If I set it to 'jsonp' I get 'Syntax Error: Unexpected Token :'.

From what I understand, jsonp is json wrapped in a function or something like that.

Is there a reasonable workaround or am I gonna have to 'suck it up' and develop a server-side atom-to-jsonp service?


Solution

  • This service either returns JSON or something like:

    variable = {...}
    

    if the param url parameter is set, but not JSON-P :(

    Using dataType: "jsonp" can not work, and dataType: "json" won't work either, because the service does not allow cross-domain ajax requests (since no Access-Control-Allow-Origin header is set -> see: CORS)

    A workaround would be to insert a <script> tag like this:

    <script type="text/javascript" src="http://www.blastcasta.com/feed-to-json.aspx?feedurl=<your url here>&param=myVariable"></script>
    

    then myVariable contains the returned object.

    Example Demo: http://jsfiddle.net/kFL9G/