首页 > 解决方案 > React Native + Expo 获取缓存控制

问题描述

我正在使用与 redux 集成的 react native expo 构建 android 应用程序。使用 fetch 方法调用 API,但始终显示缓存的结果。Cache-Control 标头不与请求一起使用。我尝试了两种类型的标题定义;

 var myHeaders = new Headers();
 myHeaders.set('Accept', 'application/json');
 myHeaders.set('Content-Type', 'application/json');
 myHeaders.set('Cache-Control', 'no-cache');

 fetch(
   "http://192.168.1.71:3000/", {
      method: 'GET',
      headers: myHeaders
 })
 .then(response => console.log(response)).catch(err=>console.log(err));

 fetch("http://192.168.1.71:3000/",{
   "method": "POST",
   headers: {
    'cache-control': 'no-cache',
    'Accept': 'application/json',
    'Content-Type': 'application/json; charset=utf-8',
 }, cache: "no-cache",})
.then(response => console.log(response))
.catch(err=>console.log(err));

但每次服务器中的 request.headers 显示:

{ accept: 'application/json',
  'content-type': 'application/json; charset=utf-8',
  host: '192.168.1.71:3000',
  connection: 'Keep-Alive',
  'accept-encoding': 'gzip',
  'user-agent': 'okhttp/3.12.1' }

服务器端没有缓存控制。难道我做错了什么?

标签: androidreact-nativefetchexpocache-control

解决方案


推荐阅读