Javascript 使用动态url从公共API获取数据
我想从这个带有参数的公共API获取数据。然后,我想在表中显示这些获取的数据。 我还有两个按钮向后和向前,用于更改URL的参数,从而在表中显示一组新数据 前进按钮向参数中添加1,后退按钮从API url的参数中减去1 这是我当前的代码: HTML代码:Javascript 使用动态url从公共API获取数据,javascript,Javascript,我想从这个带有参数的公共API获取数据。然后,我想在表中显示这些获取的数据。 我还有两个按钮向后和向前,用于更改URL的参数,从而在表中显示一组新数据 前进按钮向参数中添加1,后退按钮从API url的参数中减去1 这是我当前的代码: HTML代码: <div align="center"> <button style="display:inline" onclick="backFunc();">Back</button> <p id="
<div align="center">
<button style="display:inline" onclick="backFunc();">Back</button>
<p id="gameweek" style="display:inline;"></p>
<button style="display:inline" onclick="forwardFunc();">Forward</button>
<br>
<br>
<table id = "fixtures_table">
<tbody id ="tbody"></tbody>
</table>
</div>
返回
向前地
这是我的javascript代码:
var request = new XMLHttpRequest();
request.withCredentials = true;
var urlparam = "1";
roundnum = 0;
var gameweek;
request.open("GET", "https://api-football-
v1.p.rapidapi.com/v2/fixtures/league/524/Regular_Season_-_" + urlparam);
request.setRequestHeader("x-rapidapi-host", "api-football-v1.p.rapidapi.com");
request.setRequestHeader("x-rapidapi-key",
"733394c196mshc5cdb3fe20f1ec5p162525jsnc81f4950b7d6");
var data
request.onload = function () {
data = JSON.parse(this.response);
gameweek = document.getElementById('gameweek');
gameweek.innerHTML = data.api.fixtures[roundnum].round;
if (request.status >= 200 && request.status < 400) {
var tbody = document.getElementById('tbody');
var table = document.getElementById('fixtures_table');
data.api.fixtures.forEach(fixtures => {
var tr = document.createElement('tr');
tr.innerHTML = '<td>' + fixtures.homeTeam.team_name + '</td>' +
'<td>' + fixtures.goalsHomeTeam + '</td>' +
'<td>' + "VS" + '</td>' +
'<td>' + fixtures.awayTeam.team_name + '</td>' +
'<td>' + fixtures.goalsAwayTeam + '</td>' +
'<td>' + fixtures.status;
tbody.appendChild(tr);
table.appendChild(tbody);
});
}
console.log(data);
}
function forwardFunc(){
}
function backFunc(){
}
var-request=new-XMLHttpRequest();
request.withCredentials=true;
var urlparam=“1”;
roundnum=0;
var游戏周;
请求。打开(“获取”https://api-football-
v1.p.rapidapi.com/v2/fixtures/league/524/Regular\u seasure\u-\u“+urlparam);
setRequestHeader(“x-rapidapi-host”、“api-football-v1.p.rapidapi.com”);
setRequestHeader(“x-rapidapi-key”,
“733394c196mshc5cdb3fe20f1ec5p162525jsnc81f4950b7d6”);
var数据
request.onload=函数(){
data=JSON.parse(this.response);
gameweek=document.getElementById('gameweek');
gameweek.innerHTML=data.api.fixtures[roundnum].round;
如果(request.status>=200&&request.status<400){
var tbody=document.getElementById('tbody');
var table=document.getElementById('fixtures_table');
data.api.fixtures.forEach(fixtures=>{
var tr=document.createElement('tr');
tr.innerHTML=''+fixtures.homeTeam.team_name++
''+fixtures.goalsHomeTeam+''+
“+”对“+”+
''+fixtures.awayTeam.team_name+''+
''+fixtures.goalsAwayTeam+''+
''+状态;
t附肢儿童(tr);
表3.儿童(t身体);
});
}
控制台日志(数据);
}
函数forwardFunc(){
}
函数backFunc(){
}
但是,我不知道如何让forward和backwards函数使用更新的参数获取新的数据集,并在表中显示这些结果,而不是旧的结果