使用axios的get请求时产生缓存,致使请求的数据不是最新的。这样的场景很多,例如我们添加或修改数据成功后,立马调用查询接口,因查询接口是get请求,产生了缓存,响应数据还是之前数据,并没有新增或修改后的数据,给人感觉像是添加或修改失败,这显然是不合理的,那么如何禁止缓存呢?
用axios拦截器拦截请求,为get请求添加时间戳
axios.interceptors.request.use(function (config) {
console.log('........',config)
let token = sessionStorage.getItem('sjzcgl_token');
if (token) {
config.headers.common['token'] = token;
}
if (config.method == 'get') {
config.params = {
_t: new Date().getTime(),
...config.params
}
}
return config;
}, function (error) {
return Promise.reject(error);
})
其他方式
用post请求替代get请求
遇到项目需求。HTML页面。可拉动页面样式不变(1)
<!DOCTYPE html>
替换成
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
JS部分
function setBodyWidth(){
var barWidthHelper=document.createElement('div');
barWidthHelper.style.cssText="overflow:scroll; width:100px; height:100px;";
document.body.appendChild(barWidthHelper);
var barWidth=barWidthHelper.offsetWidth-barWidthHelper.clientWidth;
document.body.removeChild(barWidthHelper);
var bodyWidth=window.screen.availWidth-barWidth;
return bodyWidth;
}