在Spotify API上尝试POST请求时解析JSON时出错

时间:2018-11-18 02:30:46

标签: javascript node.js json ajax spotify

我目前正在使用JavaScript开发网络应用程序,该网络应用程序将创建一个新的播放列表,然后将其保存到用户的Spotify帐户中。现在,每当尝试POST请求时,我都会遇到“ JSON解析错误”的问题。下面是我的代码:

var urlString = 'https://api.spotify.com/v1/users/' + userID + '/playlists';

app.createPlaylist = (urlString) => $.ajax({
      url: urlString,
      method: 'POST',
      headers: {
        'Authorization': 'Bearer ' + accessToken
      },
      contentType: 'application/json',
      body: JSON.stringify({
        name: "test",
        public: true
      }),
      dataType: 'json',
});

谁能告诉我这是什么问题?我已经尝试过浏览该网站,但对我的问题可能没有找到任何答案,可以解决我的情况。

1 个答案:

答案 0 :(得分:0)

该错误的原因是您尚未关闭ajax调用花括号{}

var urlString = 'https://api.spotify.com/v1/users/' + userID + '/playlists';

app.createPlaylist = (urlString) => $.ajax({
  url: urlString,
  method: 'POST',
  headers: {
    'Authorization': 'Bearer ' + accessToken
  },
  contentType: 'application/json',
  body: JSON.stringify({
    name: "test",
    public: true
  }),
  dataType: 'json'
  }//here added the missing  braces
);