gpt4 book ai didi

javascript - API获取请求

转载 作者:行者123 更新时间:2023-12-02 15:19:12 25 4
gpt4 key购买 nike

我是编码新手,所以这可能是一个简单的修复,但我不确定我做错了什么。我正在构建一个使用 Spotify's API 的程序.

我不会撒谎,这不是一个 super 有用的程序哈哈,但你所做的就是输入艺术家的 ID 值,我希望它返回所有专辑的列表。以下是 id 的示例:4P0dddbxPil35MNN9G2MEX

这是我收到的错误消息:

https://api.spotify.com/v1/artists/undefined/albums?callback=jQuery111104149643396958709_1449799723013&album_type=album&_=144979972301400 (Bad Request).

有谁知道为什么我的搜索结果显示为“未定义”?

这是我的 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/未定义/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();

只需确保您没有多个具有该名称的输入,因为该选择器可能会返回多个元素。

关于javascript - API获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34215245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com