7

I simply cannot get a json response in Safari, calling SoundCloud's API.

var inputSet={url:setUrl},
    clientId={client_id:client_id};
$.getJSON( "https://api.soundcloud.com/resolve.json", $.extend(inputSet, clientId), function( data ) {
console.log(data);
});

This returns an Origin Access-Control error in Safari but not in Chrome. CORS is not working at all.

Saw CORS not working at all, implemented 'working' answer, exact same error, only Safari.

adding a callback parameter does not return that error, however returns the ajax error "parsererror" SyntaxError {} which I presume is due to the response still being json and not jsonp. This does not work in either browser.

As it stands I cannot get this cross domain request working in chrome, even though the docs

https://developers.soundcloud.com/docs#crossdomain

say I can.

Safari network tab: Safari

Safari response:

[Error] Failed to load resource: Origin https://seam.li is not allowed by Access-Control-Allow-Origin. (17235000.json, line 0)
[Error] XMLHttpRequest cannot load https://api.soundcloud.com/playlists/17235000.json?client_id=CLIENT_ID. Origin https://seam.li is not allowed by Access-Control-Allow-Origin. (seam.li, line 0)

Linked page loads fine in browser.

Community
  • 1
  • 1
Liam Bigelow
  • 789
  • 7
  • 15

1 Answers1

4

Using the other URLs seems to work fine. The issue lies with /resolve.json where it uses a 302 Redirect to send you to the right API URL and it doesn't jive with Safari.

According to the SoundCloud API Doc:

The resolve resource allows you to lookup and access API resources when you only know the SoundCloud.com URL.

If you don't need this functionality I suggest using the URL directly. Here's a working example.

$.getJSON("https://api.soundcloud.com/playlists/17235000.json?client_id=CLIENT_ID", 
    function( data ) {
        console.log(data);
});
J.P. Armstrong
  • 836
  • 1
  • 9
  • 26
  • Sadly I do need the resolve functionality. This relies on a user inputting the url to a SoundCloud set, and then the service plays it. I sadly can't reasonably ask them to input the api url of a playlist :( – Liam Bigelow Jan 09 '14 at 03:50
  • You'll have to use a CORS proxy – J.P. Armstrong Jan 09 '14 at 04:20
  • Any advice on setting one up? – Liam Bigelow Jan 09 '14 at 05:49
  • What you want to do is connect to a URL that you control, that way you can control the headers before it gets to JS. In your case, you'll want to capture the data after it does redirect. Let your server worry about it. I found this one on github: https://github.com/gr2m/CORS-Proxy – J.P. Armstrong Jan 09 '14 at 12:40
  • Indeed, though sadly the one you linked did not work. https://jsonp.jit.su/ worked for me, although I had to swap out a module due to a bug. Proxy now sits at seam.li/json/ – Liam Bigelow Jan 13 '14 at 02:38
  • In case anyone else finds this answer and like me think it implies that `/resolve.json` doesn't support `jsonp`: it does. Just use jsonp instead of json and safari won't complain about the 302 redirect. See [this answer](http://stackoverflow.com/questions/19440040/soundcloud-api-origin-mysite-is-not-allowed-by-access-control-allow-origin) – Andreas Hultgren Apr 14 '14 at 15:00
  • 1
    @AndreasHultgren Keep in mind that JSONP also has it's drawbacks. Since JSONP is a JavaScript wrapper you can run into illegal character issues http://stackoverflow.com/questions/28094214/invalid-javascript-jsonp-response-from-soundcloud-api – Andrew Winter Mar 19 '15 at 19:11