And*_*ves 22 caching fetch reactjs react-native
我正在使用react native构建一个应用程序,它使得获取调用依赖于来自服务器的最新信息.我注意到它似乎缓存了响应,如果我再次运行该fetch调用,它将返回缓存的响应,而不是来自我的服务器的新信息.
我的功能如下:
goToAll() {
AsyncStorage.getItem('FBId')
.then((value) => {
api.loadCurrentUser(value)
.then((res) => {
api.loadContent(res['RegisteredUser']['id'])
.then((res2) => {
console.log(res2);
this.props.navigator.push({
component: ContentList,
title: 'All',
passProps: {
content: res2,
user: res['RegisteredUser']['id']
}
})
});
});
})
.catch((error) => {console.log(error);})
.done();
}
Run Code Online (Sandbox Code Playgroud)
而api.js im调用的函数如下:
loadContent(userid){
let url = `http://####.com/api/loadContent?User_id=${userid}`;
return fetch(url).then((response) => response.json());
}
Run Code Online (Sandbox Code Playgroud)
man*_*sim 43
您可以设置a Header
以防止缓存请求.示例如下:
return fetch(url, {
headers: {
'Cache-Control': 'no-cache'
}
}).then(function (res) {
return res.json();
}).catch(function(error) {
console.warn('Request Failed: ', error);
});
Run Code Online (Sandbox Code Playgroud)
lan*_*lan 13
Manosim的回答并没有为我工作,但把我的路径,一种解决方案上做了工作:
fetch(url, {
headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': 0
}
})
Run Code Online (Sandbox Code Playgroud)
这钉了它.
归档时间: |
|
查看次数: |
14419 次 |
最近记录: |