我正在尝试查询我正在更新到React.js的freeCodeCamp项目的引用API.我现在正在尝试使用Fetch或Axios来查询API,但它正在缓存浏览器中的响应.我知道在$ajax中有一个{cache:false}会强制浏览器执行新请求.
有什么方法我可以用Fetch或Axios做同样的事情吗?
缓存控制设置似乎已由Axios设置为max-age:0.
这是我的代码,即查询API.
generateQuote = () => {
axios.get('https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1')
.then(response => {
const { title, content, link } = response.data[0];
console.log(title, content, link)
this.setState(() => ({ title, content, link }));
})
.catch(err => {
console.log(`${err} whilst contacting the quote API.`)
})
}
解决方法:
好的,所以我找到了解决方案.我必须在API网址上设置时间戳才能让它进行新的通话.似乎没有办法强制axios或fetch来禁用缓存.
这就是我的代码现在的样子
axios.get(`https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1×tamp=${new Date().getTime()}`)
.then(response => {
const { title, content, link } = response.data[0];
console.log(title, content, link)
this.setState(() => ({ title, content, link }));
})
.catch(err => {
console.log(`${err} whilst contacting the quote API.`)
})
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!