不在html页面上显示来自API的值,而是加载JSON

时间:2015-11-25 02:09:06

标签: javascript jquery json api soundcloud

尝试从soundcloud api中提取曲目列表。

  1. song one
  2. 歌曲二
  3. song three
  4. Gettin json回来了:

    enter image description here

    但它没有在html中显示结果

    Heres html:

    <!DOCTYPE html>
    <html>
    <head>
        <!-- bootstrap and js -->
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" title="bootstrap-css" charset="utf-8">
        <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    
        <!-- soundcloud info -->
        <script src="https://connect.soundcloud.com/sdk/sdk-3.0.0.js"></script>
        <script src="js/script.js"></script>
    </head>
    <body>
        <ol id="track-list"></ol>
    </body>
    </html>
    

    Heres js:

    SC.initialize({
        client_id: "myid",
        redirect_uri: "mycallback",
    });
    
    var userId = 39090345; // user_id of Prutsonic
    
    SC.get("/tracks", {
        user_id: userId,
        limit: 100
    }, function (tracks) {
        var tmp = '';
    
        for (var i = 0; i < tracks.length; i++) {
            tmp = '<a href="' + tracks[i].permalink_url + '">' + tracks[i].title + ' - ' + tracks[i].duration + '</a>';
    
            $("<li/>").html(tmp).appendTo("#track-list");
        }
    });
    

1 个答案:

答案 0 :(得分:1)

根据API,您似乎应该更改:}, function (tracks) { }).then(function (tracks) {。 API希望您使用promise。

SC.get("/tracks", {
    user_id: userId,
    limit: 100
}).then(function (tracks) {
    var tmp = '';

    for (var i = 0; i < tracks.length; i++) {
        tmp = '<a href="' + tracks[i].permalink_url + '">' + tracks[i].title + ' - ' + tracks[i].duration + '</a>';
        $("<li/>").html(tmp).appendTo("#track-list");
    }
});