React Native Axios.get 返回的结果不新鲜

React Native Axios.get returned results not fresh

我正在使用 axios 为我的 React Native 应用程序获取数据,但我只遇到 iOS 的问题。我能够完美地从我的服务器获取数据,但是如果我从我的 API 更改任何数据,这些更改根本不会反映在 iOS 中,只有当我重新安装应用程序时变化将会发生。我仍然无法确定导致问题的原因。这只发生在 iOS,Android 工作得很好。

获取数据代码:

axios.get('http://www.example.com/api')
.then((response) => {
  // console.log(response);
  this.setState({ data: response.data, loading: false });
});

如果我遗漏了任何信息,请告诉我。

如果已经有人问过这个问题,如果您能指出正确的方向,我将不胜感激。

非常感谢!

我的猜测是该页面是从缓存中读取的,因此您得到了一个旧副本。 您需要做的是向 link 添加日期戳,以强制应用程序加载 'fresh' 页面。 它是这样的:

 axios.get('http://www.example.com/api?dt='+(new Date()).getTime())
.then((response) => {
     // console.log(response);
     this.setState({ data: response.data, loading: false });
});  

可以通过在header.

中添加headers: {'Cache-Control': 'no-cache'}来解决
axios.get('http://www.example.com/api',
  headers: {'Cache-Control': 'no-cache'})
.then((response) => {
  // console.log(response);
  this.setState({ data: response.data, loading: false });
});