小编典典

React Native-提取调用已缓存

reactjs

我正在用本机构建一个应用程序,该应用程序将依赖于来自服务器的最新信息来进行调用。我注意到它似乎缓存了响应,如果我再次运行该访存调用,它将返回缓存的响应,而不是服务器中的新信息。

我的功能如下:

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();
}

api.js即时通讯中的函数如下:

loadContent(userid){
  let url = `http://####.com/api/loadContent?User_id=${userid}`;
  return fetch(url).then((response) => response.json());
}

阅读 360

收藏
2020-07-22

共1个答案

小编典典

您可以设置a Header以防止缓存请求。下面的例子:

return fetch(url, {
  headers: {
    'Cache-Control': 'no-cache'
  }
}).then(function (res) {
  return res.json();
}).catch(function(error) {
  console.warn('Request Failed: ', error);
});
2020-07-22