我正在try 查询一个freeCodeCamp项目的quote API,我正在更新以做出react .js.我现在try 使用FetchAxios来查询API,但它正在浏览器中缓存响应.我知道在$ajax中有一个{ cache: false }会迫使浏览器执行新的请求.

有没有办法让我用FetchAxios也能做到这一点?

cache-control设置似乎已经被Axios设置为max-age: 0.

enter image description here

这是我查询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 url上设置一个时间戳,让它进行新的调用.似乎没有办法强制axiosfetch禁用缓存.

这就是我的代码现在的样子

axios.get(`https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&timestamp=${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.`)
  })

Reactjs相关问答推荐

使用React Router多姆表单组件附加/?地址栏中基本URL的索引

使用useEffect挂钩获取数据

父组件更新后不重新呈现子组件

cypress 不能点击SPAN

使用Overpass API Endpoint计算 map 上的面积

使用Reaction钩子窗体验证可选字段

如何在NextJS中仅在刷新页面和页面淡出页面过渡时添加全屏加载器

react -无法获取函数的最新参数值

蚂蚁 Select .列表弹出窗口不会';有时不会出现

Reactjs:MUI5:MobileDateTimePicker API:如何自定义日历中的文本

onChange in useEffect 的最佳实践

React Native应用如何订阅Supabase的实时数据?

next js 13 在获取中使用标头时动态渲染而不是静态渲染?

顶点图表甜甜圈项目边框半径

用 jest 测试包裹在 redux 和 router 中的组件

刷新页面时状态改变问题

map 未显示在react 传单中

将路径数组传递给Route会引发错误

如何在props 更改时运行自定义挂钩?

useEffect 仅在跟随链接后有效,不适用于直接链接