创建播放列表spotify API

时间:2016-11-02 14:14:54

标签: javascript jquery rest spotify

我正在尝试使用Spotify API创建播放列表,并收到以下错误:

{
  "error" : {
    "status" : 400,
    "message" : "Error parsing JSON."
  }
}

我正在关注使用以下卷曲请求的example on this page

curl -X POST "https://api.spotify.com/v1/users/thelinmichael/playlists" -H "Authorization: Bearer {your access token}" -H "Content-Type: application/json" --data "{\"name\":\"A New Playlist\", \"public\":false}"   

我的代码看起来像这样

$.ajax({
   url: 'https://api.spotify.com/v1/users/{id}/playlists',
   method: "POST",
   data: {
     name: "test"
   },
   headers: {
     'Authorization': 'Bearer ' + access_token,
     'Content-Type': 'application/json'
   },
   success: function(response) {
     console.log(response);
   }
 });

有人可以告诉我做错了吗?

1 个答案:

答案 0 :(得分:0)

原因是我没有张贴有效的儿子。通过使用JSON.stringify,我能够解决这个问题。

JSON.stringify({name: "test", public: false})

完整代码如下所示:

$.ajax({
   url: 'https://api.spotify.com/v1/users/{id}/playlists',
   method: "POST",
   data: {
     name: JSON.stringify({name: "test", public: false})
   },
   headers: {
     'Authorization': 'Bearer ' + access_token,
     'Content-Type': 'application/json'
   },
   success: function(response) {
     console.log(response);
   }
 });