简体   繁体   English

从Spotify API检索匹配的歌曲URI

[英]retrieve matching song URI from Spotify API

I have been using Javascript to try and retrieve the URI of a song from the Spotify WEB API using the below code: 我一直在使用Javascript尝试使用以下代码从Spotify WEB API检索歌曲的URI:

<script type='text/javascript'>
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://ws.spotify.com/search/1/track.json?q=foo", false);
xhr.send();
var uri = (xhr.track);
</script>

I want to put in a song name as the 'q' parameter and store the uri of the top result in a variable. 我想将歌曲名称作为'q'参数并将顶部结果的uri存储在变量中。 How would I go about this? 我该怎么做?

You need to parse the response as JSON and then pick the href attribute of the first object in the tracks list. 您需要将响应解析为JSON,然后选择轨道列表中第一个对象的href属性。

In code, following your example, it could look like this: 在代码中,按照您的示例,它可能如下所示:

var uri = JSON.parse(xhr.response).tracks[0].href;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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