9

我想停止缓存native-http插件存储其缓存的 API 请求和响应以及它与我的应用程序的创建问题。

All-time API 工作正常,但是当我从服务器收到 404 或 401 错误时,它会将其缓存在我的应用程序中,然后始终会收到状态为 1 的超时错误。

为了克服这个问题,我需要卸载应用程序并重新安装它才能按预期工作。

任何想法如何停止缓存 HTTP 请求和响应?

或者如何解决状态为 1 的超时问题?

我在请求标头中尝试了以下内容,但仍然没有成功。

self.httpPlugin.setHeader('*', 'authorization', 'Bearer ' + token);
self.httpPlugin.setHeader('*', 'Cache-control', 'no-cache');
self.httpPlugin.setHeader('*', 'Cache-control', 'no-store');
self.httpPlugin.setHeader('*', 'Expires', '0');
self.httpPlugin.setHeader('*', 'Pragma', 'no-cache');

还在我的请求中添加了一个虚拟的唯一参数,以发出我的 API 调用的唯一请求,如下所示。

self.httpPlugin.setHeader('*', 'ExtraDate', new Date().toString());

有人在 Ionic 3 中遇到过这种问题吗?

尝试了这个线程建议,但一点运气都没有。

为这个问题提出任何解决方案。

编辑:

完整请求代码:

/**
   * Get Search result from server.
   */
getCaseListBySearchText(searchText: string): Observable<any> {
    let self = this;

  return Observable.create(function(observer) {
    self.getToken().then(token => {
      console.log("Token : ", token);

      // let rand = Math.random();
      self.httpPlugin.setHeader("*", "authorization", "Bearer " + token);
      self.httpPlugin.setHeader("*", "Cache-control", "no-cache");
      self.httpPlugin.setHeader("*", "Cache-control", "no-store");
      // self.httpPlugin.setHeader("*", "Expires", "0");
      self.httpPlugin.setHeader("*", "Cache-control", "max-age=0");
      self.httpPlugin.setHeader("*", "Pragma", "no-cache");
      self.httpPlugin.setHeader("*", "ExtraDate", new Date().toString());

      self.httpPlugin
        .get(self.url + "/CaseList?caseNum=" + searchText, {}, {})
        .then(response => {
          console.log("Response Success : " + JSON.stringify(response));
          let jsonResponse = JSON.parse(response.data);
          console.log("JSON OBJECT RESPONSE : " + jsonResponse);
          observer.next(jsonResponse);
        })
        .catch(error => {
          if (error.status == 403) {
            console.log("Token expired : " + JSON.stringify(error));
            self.isTokenExpired = true;
            //Removing Old Token
            self.storage.remove(Constants.AUTH_DATA);
            observer.error(error);
          } else {
            console.log("Error : " + error);
            console.log("Error " + JSON.stringify(error));
            observer.error(error);
          }
        });
    })
    .catch(error => {
      console.log("Error : " + error);
      observer.error(error);
    });
});


}
4

3 回答 3

2

在针对上述问题进行大量研发和网络搜索后,我找到了清理请求缓存的解决方案,因为最重要的是,标头无法清理缓存。

在 HTTP Advanced 插件中,有一种方法可以清除我的 cookie。

clearCookies()

清除所有 cookie。

通过在我的类构造函数中使用上述方法在调用任何 API 之前清除 cookie。

因此,它将清除我所有的 cookie 以及与旧 cookie 相关的问题将通过这种方式解决。

constructor(
    public storage: Storage,
    public httpPlugin: HTTP,
    private platform: Platform
  ) {
    // enable SSL pinning
    httpPlugin.setSSLCertMode("pinned");
    //Clear old cookies
    httpPlugin.clearCookies();
  }

上面的代码解决了我的问题。

感谢大家的快速指导和建议。

如果这不是清除我的旧请求数据的正确方法,请对此发表评论。

于 2018-10-19T11:55:59.783 回答
1

您可以查看响应标头,以确定服务器告诉您的客户端有关缓存的内容。您还可以尝试通过设置 Cache-Control: max-age=0 来强制来自服务器的新响应。

另外,我会避免将 Expires 标头设置为 0,因为这通常由服务器设置,并被定义为像“1970 年 1 月 1 日 00:00:00 GMT”这样的日期。总的来说,这三个请求头对于无缓存机制应该是足够好的。

  • 杂注:无缓存
  • 缓存控制:无缓存
  • 缓存控制:max-age=0

大多数关于 http 缓存的行为在相应的 RFC 中都有解释:

https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html

于 2018-10-16T15:43:45.583 回答
1

通常,如果您在请求的 URL 标头中添加随机数。你可以实现它。

与我分享完整的代码块,我会尽力帮助你。

于 2018-10-17T11:24:06.997 回答