React Native fetch API无法禁用缓存

Mir*_* Vu 14 react-native fetch-api react-redux

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

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
    };
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢

RAH*_*SHI 7

如何设置用于获取请求的标头一定存在问题.

尝试以下,

您可以在官方Fetch API中按照相同的链接进行操作

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());
};
Run Code Online (Sandbox Code Playgroud)