React Native fetch API 无法禁用缓存 [英] React Native fetch API cannot disable caching

查看:40
本文介绍了React Native fetch API 无法禁用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用与 redux 集成的 react native expo 构建 android 应用程序.使用 fetch 方法调用 API,但始终显示缓存的结果.服务器第二次没有收到请求.我尝试使用以下代码禁用缓存.

I am building android app using react native expo integrated with redux. The API is called using fetch method, but always the cached result is displayed. The server did not receive the request second time. I tried disabling cache with the following code.

export const mymails = (token) => {
    return fetch(
        API_URL+'?random_number='+ new Date().getTime(), {
        method: 'GET',
        headers: getHeaders(token)
    })  
    .then(response => response.json());
};

getHeaders = (token) => {
    return {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Token token='+token,
        'Cache-Control': 'no-cache, no-store, must-revalidate',
        'Pragma': 'no-cache',
        'Expires': 0
    };
}

当我通过 Postman 客户端调用 API 时,我看到不同的结果(未缓存).我尝试添加随机数作为参数并设置缓存控制标头,但仍然返回缓存结果.还有什么我可以尝试的吗.

When I call the API through Postman client I see different result(not cached). I tried adding random number as parameter and setting cache control headers, but still returning cached result. Is there is anything else I could try.

谢谢

推荐答案

你是如何设置获取请求的头部信息的.

There must be a problem with how are you setting up the headers for fetching request.

尝试以下操作,

您可以在 Official Fetch 中点击相同的链接应用程序接口

const mymails = (token) => {

    var myHeaders = new Headers();
    myHeaders.set('Accept', 'application/json');
    myHeaders.set('Content-Type', 'application/json');
    myHeaders.set('Authorization', 'Token token=' + String(token));
    myHeaders.set('Cache-Control', 'no-cache');
    myHeaders.set('Pragma', 'no-cache');
    myHeaders.set('Expires', '0');

    return fetch(
        API_URL + '?random_number=' + new Date().getTime(), {
            method: 'GET',
            headers: myHeaders
        })
        .then(response => response.json());
};

这篇关于React Native fetch API 无法禁用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆