代码之家  ›  专栏  ›  技术社区  ›  Leandro Gabrielli

使用jquery的rest api-json数据到html表

  •  1
  • Leandro Gabrielli  · 技术社区  · 6 年前

    我想通过jquery将api中的数据显示到html表中,但它没有将数据显示到表中。我认为这是由于json结构 这是HTML文件

            <thead>
                <tr>
                    <th>A</th>
                    <th>B</th>
                    <th>C</th>
                </tr>
            </thead>
            <tbody id="mydata">
                <tr>
                    <td>[[E]]</td>
                    <td>[[F]]</td>
                    <td>[[G]]</td>
                </tr>           
            </tbody>
        </table>
        </div>
    
        <script>
            function UserAction() {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
             if (this.readyState == 4 && this.status == 200) {
                 alert(this.responseText);
             }
        };
        xhttp.open("GET", "https:xxxxxxx", true);
        xhttp.setRequestHeader("Content-type", "application/json");
        xhttp.setRequestHeader("Authorization", "Bearer eyJhbGciOiJIUzI1Nixxxxxxx");
    }
        $("#mydata").mirandajs(xhhtp);
        </script>
    

    关于json响应,我想获取 player、timestamp和reason,并将数据呈现到表中(在示例e、f、g中),下面是api响应

    {
        "meta": {
            "active": 2,
            "expired": 275,
            "total": 277
        },
        "links": {
            "next": "https://api.xxxxxxx"
        },
        "data": [
            {
                "type": "xxx",
                "id": "xxxx",
                "meta": {
                    "player": "Serbo"
                },
                "attributes": {
                    "id": "xxxx",
                    "uid": "xxxx",
                    "timestamp": "2018-07-11T13:02:14.663Z",
                    "reason": "xxxxxxx",
                    "note": null,
                    "identifiers": [
                        {
                            "id": xxxxxxx,
                            "type": "xxxxxxx",
                            "identifier": "xxxxxxx",
                            "private": true,
                            "lastSeen": "2018-08-17T16:23:24.865+00:00"
                        }
                    ]
    

    请注意,我需要json对象中的数据,然后需要attributes对象中的数据

    1 回复  |  直到 6 年前
        1
  •  1
  •   Leandro Gabrielli    6 年前

    解决了,这是关于我调用api的方式