【问题标题】:API Get RequestAPI 获取请求
【发布时间】:2016-03-16 21:02:37
【问题描述】:

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

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

这是我收到的错误消息:

https://api.spotify.com/v1/artists/undefined/albums?callback=jQuery111104149643396958709_1449799723013&album_type=album&_=144979972301400(错误请求)。

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

这是我的 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);


        });



})

【问题讨论】:

  • 检查网址。您正在请求 `/artists/UNDEFINED/albums.这意味着在评估它时,artistID 为空。
  • 你在哪里调用 getArtistAlbumInformation 方法?显示该代码

标签: javascript jquery spotify


【解决方案1】:

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();

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

【讨论】:

  • 拒绝执行脚本,因为它的 MIME 类型 ('application/json') 不可执行,并且启用了严格的 MIME 类型检查。这是我收到的错误消息。
  • 啊哈,看来你原来的问题已经解决了,你已经进入下一个问题了。更多信息:stackoverflow.com/questions/24528211/…
  • dataType: "jsonp", 在这种情况下是你的问题,你想要dataType: "json",
  • 太棒了,我的问题已经解决了。我有另一个错误消息,但我想我可以处理这部分。这会给我一些东西玩一段时间。感谢大家的帮助!
【解决方案2】:

将 jQuery 查找更改为:

var userSearch = $("input[name='musicalArtist']").val();

this 指的是触发点击事件的按钮。

【讨论】:

  • 拒绝执行脚本,因为它的 MIME 类型 ('application/json') 不可执行,并且启用了严格的 MIME 类型检查。这是我收到的错误消息。
  • 如果将数据类型更改为:dataType: "json",(而不是 jsonp
  • 它有效。我收到了另一条错误消息,但我想我可以弄清楚那部分。感谢大家的帮助!
【解决方案3】:

如果你想获取专辑 ID,你需要使用 url https://api.spotify.com/v1/albums/AlbumID,如果你想获取艺术家信息,那么使用 url https://api.spotify.com/v1/artist/ArtistID 现在你正在定义一个未定义的艺术家,然后将专辑放在后面,我也尝试过对这两个都使用这个 id,但 id 是无效的,所以你需要找到正确的 id 这里是一个工作 url 来查找专辑点击这个,你会看到一个有效的响应 https://api.spotify.com/v1/albums/0sNOF9WDwhWunNAHPD3Baj?callback=jQuery111104149643396958709_1449799723013&&type=album

【讨论】:

  • 如果你点击你发布的最后一个链接,他们有“Album_type”而不是“type”,你确定吗?我对这些东西很陌生,我只是好奇。
  • 我的错,我刚刚在developer.spotify.com/web-api/get-album 快​​速阅读了文档,结果证明album_type=album 是一个有效参数,这是他们声明我必须直接阅读的第一个参数。对不起,我会从我的答案类型中删除它是一个有效参数,它也是他们最后声明的参数之一。但是您需要在 url 的开头定义艺术家或专辑,如果它在您的示例中表示未定义,那么 id 将去向
  • 那么您现在的意思是,这不是使用艺术家 ID 值,而是按照目前的方式搜索专辑 ID 值?
  • 是的,所以我看到您正在尝试获取艺术家专辑,如果您在其中正确插入了艺术家 ID,那么您现在应该得到一个有效的响应,这里是一个随机艺术家的 url 响应在api.spotify.com/v1/artists/1vCWHaC5f2uS3yhpwWbIA6/…,这与您在本文档中想要正确的内容一致developer.spotify.com/web-api/get-artists-albums 我读错了您的原始问题
  • 既然您拥有正确的 ID,您是否收到了有效的回复?还是还有问题
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 2021-05-28
  • 2017-11-26
  • 2013-11-27
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多