我想停止缓存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);
});
});
}