简体   繁体   中英

Post request to Spotify API

I'm using Node.js to build an website where I can skip tracks and queue songs in Spotify (project for learning nodeJS and Javascript)

This code in inside index.html.

document.getElementById('skip-song').addEventListener('click', function(){
    $.post({
        url: 'https://api.spotify.com/v1/me/player/next',
        headers: {
            'Authorization': 'Bearer ' + access_token
        },
        success: function(response){
            console.log(response.headers);
        }
    });
});

But, when I look on the console of the browser, it reads:

Failed to load resource: the server responded with a status of 404 (Not Found) : 8888/[object%20Object]

Question here is: should I be sending a request to the localhost and route it to spotify api or should I send a request directly to spotify like I'm doing in this code?

If it's the second, I can't really figure out why it doesn't work.

I've found out! I'm still not sure why that was not working, but by changing the code to the following it worked.

document.getElementById('skip-song').addEventListener('click', function(){
  $.ajax({
    type: 'POST',
    url:'https://api.spotify.com/v1/me/player/next',
    headers: {'Authorization': "Bearer " + access_token}
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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