API 获取请求
API Get Request
我是编码新手,所以这可能是一个简单的修复,但我不确定我做错了什么。我正在构建一个使用 Spotify's API.
的程序
我不想说谎,这不是一个超级有用的程序哈哈,但你所做的是为艺术家输入一个 ID 值,我希望它成为 return 列表所有的专辑。这是一个 id 的示例:4P0dddbxPil35MNN9G2MEX
这是我收到的错误消息:
有谁知道为什么我的搜索结果是 "undefined?"
这是我的 JavaScript/jQuery:
var showArtistInformation = function(artistSelect){
//clone the result template
var artistResult = $('.searchResults .hiddenTemplates .artistAlbumInformation').clone();
//Set the name of the artist in the result
var theArtistName = result.find('.artistName');
theArtistName.text(artists.name);
//Get the name of the artist Album
var theAlbumName = result.find('albumName');
theAlbumName.text(album.name);
//Get the genre of the album
var theAlbumGenre = result.find('albumGenre');
theAlbumGenre.text(genre.name);
//Get the tracklisting for each Album
var theTrackNames = result.find('trackList');
theTrackNames.text(track.name);
return artistResult;
}
var getArtistAlbumInformation = function(artistID){
//the parameters that we need to pass in our request to Spotify's API
var request = {
album_type: "album"
};
$.ajax({
url: "https://api.spotify.com/v1/artists/" + artistID + "/albums",
data: request,
dataType: "jsonp",
type: "GET",
})
//What the function should do if successful
.done(function(result){
$.each(result.items, function(i,item){
var artistReturnedResult = showArtistInformation(item);
$('.artistAlbumInformation').append(artistSelect);
})
})
//What the function should do if it fails
.fail(function(jqXHR, error){
var errorElem = showError(error);
$('.search-results').append(errorElem);
});
}
//Clicking the submit button activates the function that gets the results
$(document).ready(function() {
/*Clicking the Search Button*/
$('.searchButton').on('click',function(event){
event.preventDefault();
//Zero out results if previous results have run
$('.albumArtistInformation').html('');
//End User Input
var userSearch = $(this).find("input[name='musicalArtist']").val();
getArtistAlbumInformation(userSearch);
});
})
https://api.spotify.com/v1/artists/
undefined
/albums?callback=jQuery111104149643396958709_1449799723013&album_type=album&_=144979972301400
强调我的。您为函数提供的艺术家 ID 未定义。这意味着无论你从哪里调用这个函数,你的错误就在哪里。在你的代码中尝试 console.log(artistID);
你自己看看。
var userSearch = $(this).find("input[name='musicalArtist']").val();
是你的问题所在。
您正在使用 $(this)
选择,在点击功能的上下文中,它是实际的搜索按钮。所以你试图在你的按钮中找到 musicalArtist
,这是没有意义的。
试试改用这个:
var userSearch = $("input[name='musicalArtist']").val();
只要确保您没有超过一个具有该名称的输入,因为该选择器可以 return 多个元素。
将 jQuery 查找更改为:
var userSearch = $("input[name='musicalArtist']").val();
this
指的是触发点击事件的按钮。
如果你想得到专辑ID你需要使用url https://api.spotify.com/v1/albums/AlbumID and if you want to get the artists info then use the url https://api.spotify.com/v1/artist/ArtistID right now you are defining an undefined artist and then putting albums after, also I have tried using this id for both of these and the id is invalid so you need to find the correct id here is a working url to find an album click on this and you will see a valid response https://api.spotify.com/v1/albums/0sNOF9WDwhWunNAHPD3Baj?callback=jQuery111104149643396958709_1449799723013&&type=album
我是编码新手,所以这可能是一个简单的修复,但我不确定我做错了什么。我正在构建一个使用 Spotify's API.
的程序我不想说谎,这不是一个超级有用的程序哈哈,但你所做的是为艺术家输入一个 ID 值,我希望它成为 return 列表所有的专辑。这是一个 id 的示例:4P0dddbxPil35MNN9G2MEX
这是我收到的错误消息:
有谁知道为什么我的搜索结果是 "undefined?"
这是我的 JavaScript/jQuery:
var showArtistInformation = function(artistSelect){
//clone the result template
var artistResult = $('.searchResults .hiddenTemplates .artistAlbumInformation').clone();
//Set the name of the artist in the result
var theArtistName = result.find('.artistName');
theArtistName.text(artists.name);
//Get the name of the artist Album
var theAlbumName = result.find('albumName');
theAlbumName.text(album.name);
//Get the genre of the album
var theAlbumGenre = result.find('albumGenre');
theAlbumGenre.text(genre.name);
//Get the tracklisting for each Album
var theTrackNames = result.find('trackList');
theTrackNames.text(track.name);
return artistResult;
}
var getArtistAlbumInformation = function(artistID){
//the parameters that we need to pass in our request to Spotify's API
var request = {
album_type: "album"
};
$.ajax({
url: "https://api.spotify.com/v1/artists/" + artistID + "/albums",
data: request,
dataType: "jsonp",
type: "GET",
})
//What the function should do if successful
.done(function(result){
$.each(result.items, function(i,item){
var artistReturnedResult = showArtistInformation(item);
$('.artistAlbumInformation').append(artistSelect);
})
})
//What the function should do if it fails
.fail(function(jqXHR, error){
var errorElem = showError(error);
$('.search-results').append(errorElem);
});
}
//Clicking the submit button activates the function that gets the results
$(document).ready(function() {
/*Clicking the Search Button*/
$('.searchButton').on('click',function(event){
event.preventDefault();
//Zero out results if previous results have run
$('.albumArtistInformation').html('');
//End User Input
var userSearch = $(this).find("input[name='musicalArtist']").val();
getArtistAlbumInformation(userSearch);
});
})
https://api.spotify.com/v1/artists/
undefined
/albums?callback=jQuery111104149643396958709_1449799723013&album_type=album&_=144979972301400
强调我的。您为函数提供的艺术家 ID 未定义。这意味着无论你从哪里调用这个函数,你的错误就在哪里。在你的代码中尝试 console.log(artistID);
你自己看看。
var userSearch = $(this).find("input[name='musicalArtist']").val();
是你的问题所在。
您正在使用 $(this)
选择,在点击功能的上下文中,它是实际的搜索按钮。所以你试图在你的按钮中找到 musicalArtist
,这是没有意义的。
试试改用这个:
var userSearch = $("input[name='musicalArtist']").val();
只要确保您没有超过一个具有该名称的输入,因为该选择器可以 return 多个元素。
将 jQuery 查找更改为:
var userSearch = $("input[name='musicalArtist']").val();
this
指的是触发点击事件的按钮。
如果你想得到专辑ID你需要使用url https://api.spotify.com/v1/albums/AlbumID and if you want to get the artists info then use the url https://api.spotify.com/v1/artist/ArtistID right now you are defining an undefined artist and then putting albums after, also I have tried using this id for both of these and the id is invalid so you need to find the correct id here is a working url to find an album click on this and you will see a valid response https://api.spotify.com/v1/albums/0sNOF9WDwhWunNAHPD3Baj?callback=jQuery111104149643396958709_1449799723013&&type=album