【问题标题】:Accessing API resource by ID通过 ID 访问 API 资源
【发布时间】:2018-03-26 20:50:45
【问题描述】:

说这个api.football-data.org/v1/competitions包含以下内容

{
    "_links": {
      "teams": {
        "href": "http://api.football-data.org/v1/competitions/444/teams"
      }
    },
    "id": 444,
    "caption": "Campeonato Brasileiro da Série A",
    "league": "BSA"
  },
{
    "_links": {
      "teams": {
        "href": "http://api.football-data.org/v1/competitions/445/teams"
      }
    },
    "id": 445,
    "caption": "Premier League 2017/18",
    "league": "PL"
  }

访问特定团队的最佳方式是什么?我这样做,它有效,但我认为它不是很整洁。

HTML

Choose Team: <select id="team">
                <option value="444">Premier League</option>
                <option value="445">Campeonato Brasileiro</option>
            </select>

JavaScript

$("#team").change(function(){
    var id = $("#team").val();
    getTeam(id);

});

function getTeam(id){

        $.ajax({
            url: 'http://api.football-data.org/v1/competitions/'+id+'/team',
            type: 'GET',
            dataType: 'json',
            success: function(data) {
                var team= '';
                $.each(data, function(k, v) {
                    team+= '<tr><td>' + v.name+ '</td></tr>';

                });
                $("#team").html(team);

            }
        });
    }

【问题讨论】:

  • 是什么让你觉得它不是很整洁?我觉得完全可以接受
  • 您是否希望对客户端进行更多控制以与包括在内的团队竞争?如果是这样,那么 graphql 可能比 rest api 更适合你。

标签: javascript jquery ajax rest api


【解决方案1】:

您的解决方案是正确的,但您可以使用 API 返回的 URL。我认为这个 API 是根据 Richardson 成熟度模型创建的(更多信息在这里:https://martinfowler.com/articles/richardsonMaturityModel.html)。 api-football 处于第 3 级 - 带有超媒体控件 - 因为 API 告诉我们下一步可以做什么(获得球队,获得球员......)

因此,它可以为您的应用程序带来一些好处:如果 api-football 更改了某些 URL,您的应用程序不会中断。

您可以根据需要更改解决方案:

        var apiKey = '7b35712f29da4dc58a538320e57c12cc';

        $(function () {
            getCompetitions();
            $('#competitions').change(function () {
                var urlTeam = $("#competitions").find("option:selected").attr('data-href');
                getTeam(urlTeam);
            });
        })

        function getCompetitions() {

            $.ajax({
                url: 'http://api.football-data.org/v1/competitions/',
                headers: {
                    "X-Auth-Token":apiKey
                },
                type: 'GET',
                dataType: 'json',
                success: function (data) {
                    $.each(data, function (k, v) {
                        $('#competitions').append('<option value="' + v.id + '" data-href="' + v._links.teams.href + '">' + v.caption + '</option>');
                    });
                }
            });
        }

        function getTeam(url)
        {
            $.ajax({
                url: url,
                headers: {
                    "X-Auth-Token":apiKey
                },
                type: 'GET',
                dataType: 'json',
                success: function (data) {
                    var team = '';
                    $.each(data.teams, function (k, v) {
                        team += '<tr><td>' + v.name + '</td></tr>';
                    });
                    $("#teams").html(team);
                }
            });
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label> Choose Competition: </label>
<select id="competitions">
  <option>Select...</option>
</select> <br />
<label>Teams </label>
<table id="teams">
</table>

【讨论】:

    猜你喜欢
    • 2023-01-31
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 2022-09-27
    • 1970-01-01
    相关资源
    最近更新 更多