【问题标题】:create playlist spotify API创建播放列表 spotify API
【发布时间】:2017-03-15 21:29:38
【问题描述】:

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

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

我正在关注example on this page,它说要使用以下 curl 请求

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);
   }
 });

谁能告诉我我做错了什么?

【问题讨论】:

  • 再次检查您的令牌,ajax 是否正确
  • @HudsonPH 我不认为令牌有问题,因为我可以发出其他请求,只是 POST 请求吸引了我

标签: javascript jquery rest spotify


【解决方案1】:

原因是我没有发布有效的儿子。通过使用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);
   }
 });

【讨论】:

  • 奇怪,好像错误信息没有骗你;)
  • @zerohero 哈哈哈,我知道,我还是花了将近一天的时间才弄明白 :-)
  • 去过那里:P至少下次你知道
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多