Ajax放置请求不适用于数据变量集

时间:2018-11-26 21:27:02

标签: javascript jquery html ajax spotify

我正在使用Spotify Web Playback SDK,并且每当我在数据标签/部分下放置一个变量时,AJAX函数就会失败。但是,当我将变量的内容放在数据下时,它可以完美地工作。 window.Thingy等于'{"uris": ["spotify:track:2xYlyywNgefLCRDG8hlxZq","spotify:track:3FCto7hnn1shUyZL42YgfO"]}' 带有变量:

function play(device_id) {
  ListPlaylistTracks(); //Gets output of JSON and saves it to window.Thingy
  $.ajax({
   url: "https://api.spotify.com/v1/me/player/play?device_id=" + device_id,
   type: "PUT",
   data: window.Thingy, //Variable that doesnt work.
   beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer ' + _token );},
   success: function(data) { 
     window.dvcID = device_id;
     window.PlayBack = true; 
   }
  });
}

无变量:

function play(device_id) {
  ListPlaylistTracks();
  $.ajax({
   url: "https://api.spotify.com/v1/me/player/play?device_id=" + device_id,
   type: "PUT",
   data: '{"uris": ["spotify:track:2xYlyywNgefLCRDG8hlxZq","spotify:track:3FCto7hnn1shUyZL42YgfO"]}',
   beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer ' + _token );},
   success: function(data) { 
     window.dvcID = device_id;
     window.PlayBack = true;
   }
  });
}

直接比较:

data: window.Thingy,

vs

data: '{"uris": ["spotify:track:2xYlyywNgefLCRDG8hlxZq","spotify:track:3FCto7hnn1shUyZL42YgfO"]}',

非常感谢您的帮助!

0 个答案:

没有答案