简体   繁体   English

将请求发布到Spotify API

[英]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) 我正在使用Node.js构建一个网站,可以在Spotify中跳过音轨并将歌曲排队(用于学习nodeJS和Javascript的项目)

This code in inside index.html. 此代码位于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] 无法加载资源:服务器以状态404(未找到)响应: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? 这里的问题是:我应该将请求发送到localhost并将其路由到spotify api,还是应该像在此代码中那样直接将请求发送到spotify?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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