我正在尝试创建一个网站,以便以一种干净的方式显示此API提供的信息。我已经完成了设计,但现在我需要实际获取此信息。我想获得的信息来自 https://api.roleplay.co.uk/v1/player/xxx而且我可能想要替换' ...'在下面的代码中的h1中使用" name"来自API的位。我该怎么做呢?我对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>
&#13;
答案 0 :(得分:1)
您可以使用jQuery进行这样的调用:
TextView titleTV = (TextView) view.findViewById(R.id.favouriteTitle);
数据包含来自API的所有JSON信息以及您可以使用的jQuery / pure Javascript。
$.getJSON(' https://api.roleplay.co.uk/v1/player/76561198062083666', function(data) {
$('#playerName').html(data.name);
});
会改变您的h1
$('#playerName').html(data.name)
从...到吉姆
我的JSFiddle:https://jsfiddle.net/calinvlasin/dn5fhqfa/1/
答案 1 :(得分:0)
我更愿意通过实际做事来学习
只是一种方式来说,给我解决方案,然后我会试着解决它,但没关系,人们以不同的方式学习。
我更新了您的HTML,因此更容易从JavaScript引用它,我不知道report
和recommend
功能如何工作,因为它们不在API中。
<body>
<div class="playerdata">
<div class="head">
<h1 id="playerName">...</h1>
<h2 id="steamId">'STEAMID'</h2>
<button id="steamProfile">Steam Profile</button>
<button id="forumProfile">Forum Profile</button>
<button id="report">Report</button>
<button id="recommend">Recommend</button>
</div>
</div>
<script type="text/javascript">
function httpGetJSON(theUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return JSON.parse(xmlHttp.responseText);
}
var player = httpGetJSON("https://api.roleplay.co.uk/v1/player/76561198062083666");
document.getElementById('playerName').innerHTML = player.name;
document.getElementById('steamId').innerHTML = player.steamid;
document.getElementById('steamProfile').addEventListener('click', function(){
window.location.href = player.steam.profileurl
})
document.getElementById('forumProfile').addEventListener('click', function(){
window.location.href = player.forumurl
})
</script>
<body>
这应该为您提供一个使用
的模板