我有一个显示数据数组的 axios GET 请求,但一次只显示 15 个项目。API url 使用多个页面,如example.com/?page=1
. 我想创建某种分页,我可以在其中选择下一页,它将 GET 请求更改为下一页。如何根据我选择的分页号更改 url 页码?
这是我当前的 componentDidMount 请求,所以当我到达页面时,
componentDidMount() {
const access_token = JSON.parse(localStorage['appState']).user
.access_token;
const config = {
headers: {
Authorization: 'Bearer ' + access_token,
},
};
axios
.get('https://exammple.com/api/v1/job?page=1', config)
.then((response) => {
// handle success
console.log(response.data.data);
this.setState({ jobs_info: response.data.data });
})
.catch((error) => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
}