How can I do a /resolve with the Soundcloud Javascript SDK?

青春壹個敷衍的年華 提交于 2019-12-05 10:38:04

问题


I would like a user of my app to

  1. paste in a Soundcloud track URL to a <input> field
  2. press a submit <button>
  3. have a function return the track's id

So far the only way I know how to do a /resolve is via command line's curl command as demonstrated here http://developers.soundcloud.com/docs/api/reference#resolve. Is there a way to do this via a Javascript function?

e.g.

SC.resolve(trackURL, id, function(id, error){});

回答1:


All curl does is an HTTP request. JavaScript is definitely able to do an HTTP request, most common way of doing that is by using XMLHttpRequest also known as AJAX.

Popular libraries, such as jQuery simplify issuing these requests, for example:

// replace YOUR_CLIENT_ID with one you can get from http://soundcloud.com/you/apps
var trackUrl = 'http://soundcloud.com/matas/hobnotropic';
$.get(
  'http://api.soundcloud.com/resolve.json?url=' + trackUrl + '&client_id=YOUR_CLIENT_ID', 
  function (result) {
    console.log(result);
  }
);

Working example here.




回答2:


You can actually use SC.get() directly for almost every resources in the API (without jQuery). Like this:

<script src="http://connect.soundcloud.com/sdk.js"></script>

<script>

   SC.initialize({
       client_id: "YOUR_CLIENT_ID"
   });

   SC.get("/resolve/?url=https://soundcloud.com/dogwill", {limit: 1}, function(result){
       console.log(result);
   });

</script>


来源:https://stackoverflow.com/questions/18148488/how-can-i-do-a-resolve-with-the-soundcloud-javascript-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!