我正在为项目使用Spotify Web API,但是我错过了一些东西,因为我收到一个400错误,根据Documentation
,这是一个语法问题。
代码是:
function generatePlaylist(access_token) {
$.ajax({
url: 'https://api.spotify.com/v1/me/playlists',
method: "POST",
data: {"name": "New Playlist", "description": "New playlist description","public": false},
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + access_token
},
success: function(response) {
console.log("Success!");
}
});
}
这是一个新手问题,但是有人可以帮助我编写正确的JSON请求吗?问候!
答案 0 :(得分:0)
没关系,这是功能代码。只需添加JSON.stringify:
function generatePlaylist(access_token) {
$.ajax({
url: 'https://api.spotify.com/v1/me/playlists',
method: "POST",
data: JSON.stringify({name: "test", public: false}),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + access_token
},
success: function(response) {
console.log("Success!");
}
});
}