如何使用 AJAX Jquery 获取 API?

如何使用 JSON AJAX Jquery 从 API 获取数据?


我想用一个简单的 json ajax jquery 方法获取完整数据


我仍然不知道如何以简单的方式获取 API 数据


当我想获取数据时,我得到未定义


在我的代码下面:


<html>

<head>

    <title>Index</title>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    <script>

        $(document).ready(function()

         {

            var url="https://api.stackexchange.com/2.2/tags/jquery/top-answerers/all_time?site=stackoverflow";

            $.getJSON(url,function(result){

                $.each(result, function(i, data){

                    var user_id=data.user_id;

                    var profile_image=data.profile_image;

                    var post_count=data.post_count;

                    var score=data.score;

                    $("#listview").append("<img src='"+ profile_image +"'>" + user_id + post_count + score);

                });

            });

         });

    </script>

</head>


<body>

    <div id="listview">


    </div>

</body>

</html>


慕村225694
浏览 151回答 1
1回答

守着一只汪

要解析数据,您需要了解 API 的数据结构。{&nbsp; items: [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; post_count,&nbsp; &nbsp; &nbsp; score,&nbsp; &nbsp; &nbsp; user: {&nbsp; &nbsp; &nbsp; &nbsp; accept_rate,&nbsp; &nbsp; &nbsp; &nbsp; display_name,&nbsp; &nbsp; &nbsp; &nbsp; link, // To profile page&nbsp; &nbsp; &nbsp; &nbsp; profile_image, // A URL&nbsp; &nbsp; &nbsp; &nbsp; reputation,&nbsp; &nbsp; &nbsp; &nbsp; user_id, // A Number&nbsp; &nbsp; &nbsp; &nbsp; user_type // E.G. "moderator"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; // More of the sample object above.&nbsp; ],&nbsp; quota_max,&nbsp; quote_remaining}此代码段有一些更改,您可以通过查看上面的结构来理解这些更改。<html><head>&nbsp; &nbsp; <title>Index</title>&nbsp; &nbsp; <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; var url="https://api.stackexchange.com/2.2/tags/jquery/top-answerers/all_time?site=stackoverflow";&nbsp; &nbsp; &nbsp; $.getJSON(url,function(result){&nbsp; &nbsp; &nbsp; console.log(result)&nbsp; &nbsp; &nbsp; &nbsp; // loop through results.items NOT results&nbsp; &nbsp; &nbsp; &nbsp; $.each(result.items, function(i, data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var user_id=data.user.user_id; // NOT data.user_id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var profile_image=data.user.profile_image; // NOT data.profile_image&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var post_count=data.post_count;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var score=data.score;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#listview").append(`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><img src="${profile_image}"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserID: ${user_id}<br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Post Count: ${post_count}<br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Score: ${score}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; </script></head><body>&nbsp; &nbsp; <div id="listview">&nbsp; &nbsp; </div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript