Javascript 邮递https://accounts.spotify.com/api/token 415使用fetch时不支持的媒体

Javascript 邮递https://accounts.spotify.com/api/token 415使用fetch时不支持的媒体,javascript,json,http,Javascript,Json,Http,我试图使用fetch请求从spotify api获取授权代码,但一直收到415错误代码 class CodeHandler { constructor(Args){ this._args = Args; this.opts = { data: { name: "test", public: "false", grant_type: 'authorization_code', code: this._args

我试图使用fetch请求从spotify api获取授权代码,但一直收到415错误代码

class CodeHandler {
  constructor(Args){
    this._args = Args;
    this.opts = {
       data: {
         name: "test",
         public: "false",
     grant_type: 'authorization_code',
     code: this._args[1],
     redirect_uri: 'http%3A%2F%2Ftest.openjukeboxdev.info%2F'
       },
       headers: {
         'Authorization': 'Basic ZjRmMTk0MTIzOTM3NDgyMmI5Mjg3OGY4YTUzYjUwMjk6MDVlODA3ZDk0NDljNGE1MmFlMzM1YTQxOTlhMjMzYmI'
       }}
  }

  response() {
    console.log(this.opts)
    console.log(JSON.stringify(this.opts))
    return this._args[1];
  }

  getAccessToken(){
    const url = "https://accounts.spotify.com/api/token";

    fetch(url, {
      method: 'post',
      body: JSON.stringify(this.opts)
    })
   .then(function (data) {
      console.log('Request succeeded with JSON response', data);
    })
    .catch(function (error) {
      console.log('Request failed', error);
    }); 
  }
  pausePlayback(){
    const http = new XMLHttpRequest();
    const url = 
    http.open("POST", url);
    http.send();
    alert(http.responseText);
  }
}
这是全部错误。当我试图提出post请求时,我得到了

Response {type: "cors", url: "https://accounts.spotify.com/api/token", redirected: false, status: 415, ok: false, …}
type: "cors"
url: "https://accounts.spotify.com/api/token"
redirected: false
status: 415
ok: false
statusText: ""
headers: Headers
__proto__: Headers
body: (...)
bodyUsed: false
__proto__: Response

415表示您发送的数据的内容类型不正确。

415-不受支持的媒体
是一个客户端错误,表示服务器拒绝接受请求,因为请求负载的格式不受支持


可能的错误可能是由于错误的
内容类型
内容编码
造成的。您的请求似乎是
JSON
格式。尝试将
内容类型设置为
应用程序json
(如果还没有),看看这是否解决了问题。

我添加了“内容类型”:“application/x-www-form-urlencoded;charset=UTF-8”并对我的json正文进行了编码,现在一切都正常了

我假设您已经找到了415的意思,在spotify API文档中,针对您正在使用的特定API端点?上面说了什么?是的,我想我的头球搞乱了。我现在就试试。我在CORS上也有一些错误,所以我必须创建一个代理api来与SpotifyAPI通信。