javascript - 如何强制 axios 不在 GET 请求中缓存
问题描述
我在我的 React-Native 应用程序中使用 axios
首先,配置标题
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";
}
以下请求返回的数据是旧数据,而不是我数据库中的当前数据
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 };
});
}
解决方案
您可以尝试在 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 };
});
}
推荐阅读
- python - Pyomo 冷冻求解器
- amazon-web-services - 验证 AWS EC2 限制的准确方法
- karate - 如何验证 X509 证书身份验证的 karate.configure(SSL) 是否成功
- html - Bootstrap 4:如何在标签附近使用完整的跨度区域
- python - 将带有分组数据的 CSV 导入 Pandas 数据框
- swift - 背景颜色变化不可见
- firebase - Firebase 函数的依赖项何时安装在托管服务器上?部署时?
- azure - Azure VM上的Tomcat使用terraform?
- robotframework - Robot Framework:从文件中读取登录凭证
- java - Table View Javafx 无法正确显示数据。表中的数据是不可见的