使用JavaScript Axios / Fetch。你能禁用浏览器缓存吗? [英] Using JavaScript Axios/Fetch. Can you disable browser cache?

查看:600
本文介绍了使用JavaScript Axios / Fetch。你能禁用浏览器缓存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询我正在更新到React.js的freeCodeCamp项目的引用API。我现在尝试使用获取 Axios 来查询API,但它正在缓存浏览器中的响应。我知道在 $ ajax 中有一个 {cache:false} 会强制浏览器执行新请求。

I am trying to query a quote API for a freeCodeCamp project I'm updating to React.js. I am now trying to use Fetch or Axios to query the API but it's caching the response in the browser. I know in $ajax there is a { cache: false } that would force the browser to do a new request.

我有什么方法可以用获取 Axios做同样的事情

Is there some way I will be able to do the same with Fetch or Axios?

缓存控制设置似乎已设置为 max-age:0 by Axios

The cache-control setting seems to be already set to max-age: 0 by Axios.

这是我的代码,我正在查询API。

This is my code I have that is querying the 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 来禁用缓存。

Okay so I found a solution. I had to set a timestamp on the API url to get it to make a new call. There doesn't seem to be a way to force axios or fetch to disable cache.

这是我的代码现在的样子

This is how my code now looks

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.`)
  })

这篇关于使用JavaScript Axios / Fetch。你能禁用浏览器缓存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆