【发布时间】:2021-01-21 13:38:14
【问题描述】:
我正在使用 Spotify REST API 和 JQuery 开发一个简单的单页应用程序,它允许用户搜索艺术家并在页面上保存艺术家的信息。但是应用程序应该只保存一次特定艺术家的信息(例如,不应该有 2x Beatles)。源代码如下。谢谢!
addButton.click(() => {
// .....
$.ajax({
url: `${baseUrl}search?q=${searchQuery}&type=artist`,
type: 'GET',
datatype: 'json',
headers: {
'Authorization': 'Bearer ' + accessToken
}
}).done((resp) => {
const rawArtistName = (resp.artists.items[0].name);
const imageUrl = (resp.artists.items[0].images[0].url);
const followers = (resp.artists.items[0].followers.total);
const artistId = (resp.artists.items[0].id);
const artistWrapper = $(`<div class ="artist-stats"><div>`);
const artistImage = $(`<img src="${`${imageUrl}`}" alt="queen" width="200", height="200">`);
const artistName = $(`<p id="nameNumber">${rawArtistName}</p>`);
const artistFollowers = $(`<p> followers: ${followers} </p>`);
const deleteArtist = $(`<button id="delete-button"> go away </button>`);
artistList.append(artistWrapper);
artistWrapper.append(artistImage);
artistWrapper.append(artistName);
artistWrapper.append(artistFollowers);
artistWrapper.append(deleteArtist);
const existingArtistNames = $(' #nameNumber')
// this is my attempt ->
for (let i = 0; i < existingArtistNames.length; i++) {
const existingArtistName = existingArtistNames[i].text();
if ( something === something) {
alert('artist is already here');
return false;
}
}
【问题讨论】:
标签: javascript jquery ajax api spotify