简体   繁体   中英

Soundcloud API - Origin <mysite> is not allowed by Access-Control-Allow-Origin

As a follow-up to Play playlist or track by permalink not trackid : the solution provided works fine seemingly everywhere except Safari on Mac.

On the Safari on Mac we see XMLHttpRequest cannot load http://api.soundcloud.com/playlists/ .json?client_id=..., Origin is not allowed by Access-Control-Allow-Origin

This is somewhat complicated by the fact that we have embedded the JavaScript into another site (Confluence). The standalone fiddle worked fine on Safari.

From XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin I suspect the problem is something to do with jsonp, but really this is out of my expertise.

Any advice appreciated.

Thanks, Martin.

You can try using the JSONP approach. Here's an updated fiddle using JSONP instead of XMLHttpRequest. The main difference is in the getSoundCloudId() method and the addition of a global jsonpResponse() method.

function getSoundCloudId(permalink) {
  var jsonp   = document.createElement('script');
  var script  = document.getElementsByTagName('script')[0];
  jsonp.type  = 'text/javascript';
  jsonp.async = true;
  jsonp.src   = 'http://api.soundcloud.com/resolve.json?client_id='+CLIENT_ID 
              + '&url='+permalink+'&callback=jsonpResponse';
  script.parentNode.insertBefore(jsonp, script);
  return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM