我正在尝试创建一个网站,以简洁的方式显示此 API 提供的信息。我已经完成了设计,但现在我需要实际获取这些信息。我想要获取的信息来自 https://api.roleplay.co.uk/v1/player/xxx我可能想用 API 中的“名称”位替换下面代码中 h1 中的“...”。我该如何去做呢?我对 JS 一无所知,我更喜欢通过实际操作来学习。 这是我的相关代码片段:
<div class=playerdata>
<div class=head>
<h1 id="playerName">...</h1>
<h2>'STEAMID'</h2>
<button>Steam Profile</button>
<button>Forum Profile</button>
<button>Report</button>
<button>Recommend</button>
</div>
最佳答案
您可以使用 jQuery 进行如下调用:
$.getJSON(' https://api.roleplay.co.uk/v1/player/76561198062083666', function(data) {
$('#playerName').html(data.name);
});
data 包含来自 API 的所有 JSON 信息,您可以通过 jQuery/纯 Javascript 使用它。
$('#playerName').html(data.name)
像这样更改你的 h1
<h1 id="playerName">...</h1> to <h1 id="playerName">jim</h1>
从...到吉姆
我的 JSFiddle: https://jsfiddle.net/calinvlasin/dn5fhqfa/1/
关于javascript - 如何在我的网站上使用 javascript API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47462948/