为什么api数据不希望显示在索引页上?

时间:2019-05-19 16:03:27

标签: javascript ajax

如何通过我的网站连接api,我尝试使其建立连接,但这不起作用。

$.getJSON("https://www.scorebat.com/video-api/v1/",function(data){
console.log(data);
var title = data.side1[0].name;
$(".title").append(title);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

<script src="script.js"></script>

<p class="title"></p>

1 个答案:

答案 0 :(得分:0)

我认为应该是

data[0].side1.name

因为数据是

[
    {
        "title": "AC Milan - Frosinone",
        "embed": "<iframe src='https:\/\/www.scorebat.com\/emb...",
        "url": "https:\/\/www.scorebat.com\/ac-milan-vs-frosinone-live-stream\/",
        "thumbnail": "https:\/\/www.scorebat.com\/og\/m\/og760535.jpeg",
        "date": "2019-05-19T16:00:00+0000",
        "side1": {
            "name": "AC Milan",
            "url": "https:\/\/www.scorebat.com\/live-stream\/ac-milan\/"
        },
        "side2": {
            "name": "Frosinone",
            "url": "https:\/\/www.scorebat.com\/live-stream\/frosinone\/"
        },
        "competition": {
            "name": "ITALY: Serie A",
            "id": 13,
            "url": "https:\/\/www.scorebat.com\/italy-serie-a-live-scores\/"
        },
        ...
    }
]

因此,我们首先访问数组 data [0] 的第一个元素,并获取其属性 .side1 ,这是另一个javascript对象,然后我们获得name属性该对象的。名称