简体   繁体   English

如何强制 axios 不在 GET 请求中缓存

[英]How to force axios to not cache in GET requests

I use axios in my React-Native app我在我的 React-Native 应用程序中使用 axios

first, configure the headers首先,配置标头

function configHeaders()
{
    // I tested all these below 3 lines , no on worked
    axios.defaults.headers.common["Pragma"] = "no-cache";
    axios.defaults.headers.common["Cache-Control"] = "no-cache";
    axios.defaults.headers.common["Cache-Control"] = "no-store";
}

the data returned from the below request is old data, not the current data in my database以下请求返回的数据是旧数据,而不是我数据库中的当前数据

exports.getInfo = async function ()
{
    configHeaders();
    return await cachios.get(URL + '/user/info').then(data => {
        return { success : true , data : data.data.data.user };
    }).catch(err => {
        return { success : false , message : err.response.data.message };
    });
}

You can try to add cache buster in the URL - forcing each request to consider as new:您可以尝试在 URL 中添加缓存破坏器 - 强制将每个请求视为新请求:

const cacheBuster = (url) => `${url}?cb=${Date.now()}`;
exports.getInfo = async function ()
{
    configHeaders();
    return await cachios.get(cacheBuster(URL + '/user/info')).then(data => {
        return { success : true , data : data.data.data.user };
    }).catch(err => {
        return { success : false , message : err.response.data.message };
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM