Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  113] [ 2]  / answers: 1 / hits: 78954  / 6 Years ago, tue, march 13, 2018, 12:00:00

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.



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



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



enter



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


}


More From » reactjs

 Answers
31

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

[#54950] Friday, March 9, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sashacitlallik

Total Points: 30
Total Questions: 100
Total Answers: 85

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
sashacitlallik questions
Tue, Jun 16, 20, 00:00, 4 Years ago
Thu, Feb 20, 20, 00:00, 4 Years ago
Mon, Nov 25, 19, 00:00, 5 Years ago
;